...
1
2
3
4
5
6 package hipchat
7
8 import (
9 "encoding/json"
10 "errors"
11
12 "golang.org/x/oauth2"
13 "golang.org/x/oauth2/clientcredentials"
14 )
15
16
17 var Endpoint = oauth2.Endpoint{
18 AuthURL: "https://www.hipchat.com/users/authorize",
19 TokenURL: "https://api.hipchat.com/v2/oauth/token",
20 }
21
22
23
24 func ServerEndpoint(host string) oauth2.Endpoint {
25 return oauth2.Endpoint{
26 AuthURL: "https://" + host + "/users/authorize",
27 TokenURL: "https://" + host + "/v2/oauth/token",
28 }
29 }
30
31
32
33
34
35
36 func ClientCredentialsConfigFromCaps(capsJSON []byte, clientID, clientSecret string, scopes ...string) (*clientcredentials.Config, error) {
37 var caps struct {
38 Caps struct {
39 Endpoint struct {
40 TokenURL string `json:"tokenUrl"`
41 } `json:"oauth2Provider"`
42 } `json:"capabilities"`
43 }
44
45 if err := json.Unmarshal(capsJSON, &caps); err != nil {
46 return nil, err
47 }
48
49
50 if caps.Caps.Endpoint.TokenURL == "" {
51 return nil, errors.New("oauth2/hipchat: missing OAuth2 token URL in the capabilities descriptor JSON")
52 }
53
54 return &clientcredentials.Config{
55 ClientID: clientID,
56 ClientSecret: clientSecret,
57 Scopes: scopes,
58 TokenURL: caps.Caps.Endpoint.TokenURL,
59 }, nil
60 }
61
View as plain text