Skip to content

Commit 9ef2edd

Browse files
SamWhitedbradfitz
authored andcommitted
hipchat: Generate Config for Connect integrations
Adds a function to generate a clientcredentials.Config from a HipChat Connect addon's capability descriptor and a provided application secret. Useful when writing addons. Change-Id: I4578f5683aa9ed728e503cb3b08ff0bf743a62a4 Reviewed-on: https://go-review.googlesource.com/22397 Reviewed-by: Brad Fitzpatrick <[email protected]>
1 parent 7e9cd5d commit 9ef2edd

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

hipchat/hipchat.go

+35
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,11 @@
66
package hipchat // import "golang.org/x/oauth2/hipchat"
77

88
import (
9+
"encoding/json"
10+
"errors"
11+
912
"golang.org/x/oauth2"
13+
"golang.org/x/oauth2/clientcredentials"
1014
)
1115

1216
// Endpoint is HipChat's OAuth 2.0 endpoint.
@@ -23,3 +27,34 @@ func ServerEndpoint(host string) oauth2.Endpoint {
2327
TokenURL: "https://" + host + "/v2/oauth/token",
2428
}
2529
}
30+
31+
// ClientCredentialsConfigFromCaps generates a Config from a HipChat API
32+
// capabilities descriptor. It does not verify the scopes against the
33+
// capabilities document at this time.
34+
//
35+
// For more information see: https://www.hipchat.com/docs/apiv2/method/get_capabilities
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+
// Verify required fields.
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+
}

0 commit comments

Comments
 (0)