Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
 
 
 

61 Zeilen
1.8 KiB

  1. // Copyright 2016 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. // Package hipchat provides constants for using OAuth2 to access HipChat.
  5. package hipchat // import "golang.org/x/oauth2/hipchat"
  6. import (
  7. "encoding/json"
  8. "errors"
  9. "golang.org/x/oauth2"
  10. "golang.org/x/oauth2/clientcredentials"
  11. )
  12. // Endpoint is HipChat's OAuth 2.0 endpoint.
  13. var Endpoint = oauth2.Endpoint{
  14. AuthURL: "https://www.hipchat.com/users/authorize",
  15. TokenURL: "https://api.hipchat.com/v2/oauth/token",
  16. }
  17. // ServerEndpoint returns a new oauth2.Endpoint for a HipChat Server instance
  18. // running on the given domain or host.
  19. func ServerEndpoint(host string) oauth2.Endpoint {
  20. return oauth2.Endpoint{
  21. AuthURL: "https://" + host + "/users/authorize",
  22. TokenURL: "https://" + host + "/v2/oauth/token",
  23. }
  24. }
  25. // ClientCredentialsConfigFromCaps generates a Config from a HipChat API
  26. // capabilities descriptor. It does not verify the scopes against the
  27. // capabilities document at this time.
  28. //
  29. // For more information see: https://www.hipchat.com/docs/apiv2/method/get_capabilities
  30. func ClientCredentialsConfigFromCaps(capsJSON []byte, clientID, clientSecret string, scopes ...string) (*clientcredentials.Config, error) {
  31. var caps struct {
  32. Caps struct {
  33. Endpoint struct {
  34. TokenURL string `json:"tokenUrl"`
  35. } `json:"oauth2Provider"`
  36. } `json:"capabilities"`
  37. }
  38. if err := json.Unmarshal(capsJSON, &caps); err != nil {
  39. return nil, err
  40. }
  41. // Verify required fields.
  42. if caps.Caps.Endpoint.TokenURL == "" {
  43. return nil, errors.New("oauth2/hipchat: missing OAuth2 token URL in the capabilities descriptor JSON")
  44. }
  45. return &clientcredentials.Config{
  46. ClientID: clientID,
  47. ClientSecret: clientSecret,
  48. Scopes: scopes,
  49. TokenURL: caps.Caps.Endpoint.TokenURL,
  50. }, nil
  51. }