Skip to content

Commit eca8207

Browse files
wolfeidaubradfitz
authored andcommitted
endpoints: add new package for oauth2.Endpoint values
As per discussion in #401 and gerrit I have built out the proposed endpoint package. I migrated all the existing endpoints, not sure if you wanted this but it does illustrate the pattern. Change-Id: I53f56a06207633b2380b7cd7332cd56f9ef6578f GitHub-Last-Rev: fde9e7b GitHub-Pull-Request: #402 Reviewed-on: https://go-review.googlesource.com/c/oauth2/+/212223 Reviewed-by: Brad Fitzpatrick <[email protected]> Run-TryBot: Brad Fitzpatrick <[email protected]> TryBot-Result: Gobot Gobot <[email protected]>
1 parent 858c2ad commit eca8207

File tree

2 files changed

+281
-0
lines changed

2 files changed

+281
-0
lines changed

endpoints/endpoints.go

Lines changed: 238 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,238 @@
1+
// Copyright 2019 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+
5+
// Package endpoints provides constants for using OAuth2 to access various service.
6+
package endpoints
7+
8+
import (
9+
"strings"
10+
11+
"golang.org/x/oauth2"
12+
)
13+
14+
// Amazon is the endpoint for Amazon.
15+
var Amazon = oauth2.Endpoint{
16+
AuthURL: "https://www.amazon.com/ap/oa",
17+
TokenURL: "https://api.amazon.com/auth/o2/token",
18+
}
19+
20+
// Bitbucket is the endpoint for Bitbucket.
21+
var Bitbucket = oauth2.Endpoint{
22+
AuthURL: "https://bitbucket.org/site/oauth2/authorize",
23+
TokenURL: "https://bitbucket.org/site/oauth2/access_token",
24+
}
25+
26+
// Cern is the endpoint for CERN.
27+
var Cern = oauth2.Endpoint{
28+
AuthURL: "https://oauth.web.cern.ch/OAuth/Authorize",
29+
TokenURL: "https://oauth.web.cern.ch/OAuth/Token",
30+
}
31+
32+
// Facebook is the endpoint for Facebook.
33+
var Facebook = oauth2.Endpoint{
34+
AuthURL: "https://www.facebook.com/v3.2/dialog/oauth",
35+
TokenURL: "https://graph.facebook.com/v3.2/oauth/access_token",
36+
}
37+
38+
// Foursquare is the endpoint for Foursquare.
39+
var Foursquare = oauth2.Endpoint{
40+
AuthURL: "https://foursquare.com/oauth2/authorize",
41+
TokenURL: "https://foursquare.com/oauth2/access_token",
42+
}
43+
44+
// Fitbit is the endpoint for Fitbit.
45+
var Fitbit = oauth2.Endpoint{
46+
AuthURL: "https://www.fitbit.com/oauth2/authorize",
47+
TokenURL: "https://api.fitbit.com/oauth2/token",
48+
}
49+
50+
// GitHub is the endpoint for Github.
51+
var GitHub = oauth2.Endpoint{
52+
AuthURL: "https://github.com/login/oauth/authorize",
53+
TokenURL: "https://github.com/login/oauth/access_token",
54+
}
55+
56+
// GitLab is the endpoint for GitLab.
57+
var GitLab = oauth2.Endpoint{
58+
AuthURL: "https://gitlab.com/oauth/authorize",
59+
TokenURL: "https://gitlab.com/oauth/token",
60+
}
61+
62+
// Google is the endpoint for Google.
63+
var Google = oauth2.Endpoint{
64+
AuthURL: "https://accounts.google.com/o/oauth2/auth",
65+
TokenURL: "https://oauth2.googleapis.com/token",
66+
}
67+
68+
// Heroku is the endpoint for Heroku.
69+
var Heroku = oauth2.Endpoint{
70+
AuthURL: "https://id.heroku.com/oauth/authorize",
71+
TokenURL: "https://id.heroku.com/oauth/token",
72+
}
73+
74+
// HipChat is the endpoint for HipChat.
75+
var HipChat = oauth2.Endpoint{
76+
AuthURL: "https://www.hipchat.com/users/authorize",
77+
TokenURL: "https://api.hipchat.com/v2/oauth/token",
78+
}
79+
80+
// Instagram is the endpoint for Instagram.
81+
var Instagram = oauth2.Endpoint{
82+
AuthURL: "https://api.instagram.com/oauth/authorize",
83+
TokenURL: "https://api.instagram.com/oauth/access_token",
84+
}
85+
86+
// KaKao is the endpoint for KaKao.
87+
var KaKao = oauth2.Endpoint{
88+
AuthURL: "https://kauth.kakao.com/oauth/authorize",
89+
TokenURL: "https://kauth.kakao.com/oauth/token",
90+
}
91+
92+
// LinkedIn is the endpoint for LinkedIn.
93+
var LinkedIn = oauth2.Endpoint{
94+
AuthURL: "https://www.linkedin.com/oauth/v2/authorization",
95+
TokenURL: "https://www.linkedin.com/oauth/v2/accessToken",
96+
}
97+
98+
// Mailchimp is the endpoint for Mailchimp.
99+
var Mailchimp = oauth2.Endpoint{
100+
AuthURL: "https://login.mailchimp.com/oauth2/authorize",
101+
TokenURL: "https://login.mailchimp.com/oauth2/token",
102+
}
103+
104+
// Mailru is the endpoint for Mail.Ru.
105+
var Mailru = oauth2.Endpoint{
106+
AuthURL: "https://o2.mail.ru/login",
107+
TokenURL: "https://o2.mail.ru/token",
108+
}
109+
110+
// MediaMath is the endpoint for MediaMath.
111+
var MediaMath = oauth2.Endpoint{
112+
AuthURL: "https://api.mediamath.com/oauth2/v1.0/authorize",
113+
TokenURL: "https://api.mediamath.com/oauth2/v1.0/token",
114+
}
115+
116+
// MediaMathSandbox is the endpoint for MediaMath Sandbox.
117+
var MediaMathSandbox = oauth2.Endpoint{
118+
AuthURL: "https://t1sandbox.mediamath.com/oauth2/v1.0/authorize",
119+
TokenURL: "https://t1sandbox.mediamath.com/oauth2/v1.0/token",
120+
}
121+
122+
// Microsoft is the endpoint for Microsoft.
123+
var Microsoft = oauth2.Endpoint{
124+
AuthURL: "https://login.live.com/oauth20_authorize.srf",
125+
TokenURL: "https://login.live.com/oauth20_token.srf",
126+
}
127+
128+
// NokiaHealth is the endpoint for Nokia Health.
129+
var NokiaHealth = oauth2.Endpoint{
130+
AuthURL: "https://account.health.nokia.com/oauth2_user/authorize2",
131+
TokenURL: "https://account.health.nokia.com/oauth2/token",
132+
}
133+
134+
// Odnoklassniki is the endpoint for Odnoklassniki.
135+
var Odnoklassniki = oauth2.Endpoint{
136+
AuthURL: "https://www.odnoklassniki.ru/oauth/authorize",
137+
TokenURL: "https://api.odnoklassniki.ru/oauth/token.do",
138+
}
139+
140+
// PayPal is the endpoint for PayPal.
141+
var PayPal = oauth2.Endpoint{
142+
AuthURL: "https://www.paypal.com/webapps/auth/protocol/openidconnect/v1/authorize",
143+
TokenURL: "https://api.paypal.com/v1/identity/openidconnect/tokenservice",
144+
}
145+
146+
// PayPalSandbox is the endpoint for PayPal Sandbox.
147+
var PayPalSandbox = oauth2.Endpoint{
148+
AuthURL: "https://www.sandbox.paypal.com/webapps/auth/protocol/openidconnect/v1/authorize",
149+
TokenURL: "https://api.sandbox.paypal.com/v1/identity/openidconnect/tokenservice",
150+
}
151+
152+
// Slack is the endpoint for Slack.
153+
var Slack = oauth2.Endpoint{
154+
AuthURL: "https://slack.com/oauth/authorize",
155+
TokenURL: "https://slack.com/api/oauth.access",
156+
}
157+
158+
// Spotify is the endpoint for Spotify.
159+
var Spotify = oauth2.Endpoint{
160+
AuthURL: "https://accounts.spotify.com/authorize",
161+
TokenURL: "https://accounts.spotify.com/api/token",
162+
}
163+
164+
// StackOverflow is the endpoint for Stack Overflow.
165+
var StackOverflow = oauth2.Endpoint{
166+
AuthURL: "https://stackoverflow.com/oauth",
167+
TokenURL: "https://stackoverflow.com/oauth/access_token",
168+
}
169+
170+
// Twitch is the endpoint for Twitch.
171+
var Twitch = oauth2.Endpoint{
172+
AuthURL: "https://id.twitch.tv/oauth2/authorize",
173+
TokenURL: "https://id.twitch.tv/oauth2/token",
174+
}
175+
176+
// Uber is the endpoint for Uber.
177+
var Uber = oauth2.Endpoint{
178+
AuthURL: "https://login.uber.com/oauth/v2/authorize",
179+
TokenURL: "https://login.uber.com/oauth/v2/token",
180+
}
181+
182+
// Vk is the endpoint for Vk.
183+
var Vk = oauth2.Endpoint{
184+
AuthURL: "https://oauth.vk.com/authorize",
185+
TokenURL: "https://oauth.vk.com/access_token",
186+
}
187+
188+
// Yahoo is the endpoint for Yahoo.
189+
var Yahoo = oauth2.Endpoint{
190+
AuthURL: "https://api.login.yahoo.com/oauth2/request_auth",
191+
TokenURL: "https://api.login.yahoo.com/oauth2/get_token",
192+
}
193+
194+
// Yandex is the endpoint for Yandex.
195+
var Yandex = oauth2.Endpoint{
196+
AuthURL: "https://oauth.yandex.com/authorize",
197+
TokenURL: "https://oauth.yandex.com/token",
198+
}
199+
200+
// AzureAD returns a new oauth2.Endpoint for the given tenant at Azure Active Directory.
201+
// If tenant is empty, it uses the tenant called `common`.
202+
//
203+
// For more information see:
204+
// https://docs.microsoft.com/en-us/azure/active-directory/develop/active-directory-v2-protocols#endpoints
205+
func AzureAD(tenant string) oauth2.Endpoint {
206+
if tenant == "" {
207+
tenant = "common"
208+
}
209+
return oauth2.Endpoint{
210+
AuthURL: "https://login.microsoftonline.com/" + tenant + "/oauth2/v2.0/authorize",
211+
TokenURL: "https://login.microsoftonline.com/" + tenant + "/oauth2/v2.0/token",
212+
}
213+
}
214+
215+
// HipChatServer returns a new oauth2.Endpoint for a HipChat Server instance
216+
// running on the given domain or host.
217+
func HipChatServer(host string) oauth2.Endpoint {
218+
return oauth2.Endpoint{
219+
AuthURL: "https://" + host + "/users/authorize",
220+
TokenURL: "https://" + host + "/v2/oauth/token",
221+
}
222+
}
223+
224+
// AWSCognito returns a new oauth2.Endpoint for the supplied AWS Cognito domain which is
225+
// linked to your Cognito User Pool.
226+
//
227+
// Example domain: https://testing.auth.us-east-1.amazoncognito.com
228+
//
229+
// For more information see:
230+
// https://docs.aws.amazon.com/cognito/latest/developerguide/cognito-user-pools-assign-domain.html
231+
// https://docs.aws.amazon.com/cognito/latest/developerguide/cognito-userpools-server-contract-reference.html
232+
func AWSCognito(domain string) oauth2.Endpoint {
233+
domain = strings.TrimRight(domain, "/")
234+
return oauth2.Endpoint{
235+
AuthURL: domain + "/oauth2/authorize",
236+
TokenURL: domain + "/oauth2/token",
237+
}
238+
}

endpoints/endpoints_test.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// Copyright 2019 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+
5+
package endpoints
6+
7+
import (
8+
"testing"
9+
10+
"golang.org/x/oauth2"
11+
)
12+
13+
func TestAWSCognitoEndpoint(t *testing.T) {
14+
15+
var endpointTests = []struct {
16+
in string
17+
out oauth2.Endpoint
18+
}{
19+
{
20+
in: "https://testing.auth.us-east-1.amazoncognito.com",
21+
out: oauth2.Endpoint{
22+
AuthURL: "https://testing.auth.us-east-1.amazoncognito.com/oauth2/authorize",
23+
TokenURL: "https://testing.auth.us-east-1.amazoncognito.com/oauth2/token",
24+
},
25+
},
26+
{
27+
in: "https://testing.auth.us-east-1.amazoncognito.com/",
28+
out: oauth2.Endpoint{
29+
AuthURL: "https://testing.auth.us-east-1.amazoncognito.com/oauth2/authorize",
30+
TokenURL: "https://testing.auth.us-east-1.amazoncognito.com/oauth2/token",
31+
},
32+
},
33+
}
34+
35+
for _, tt := range endpointTests {
36+
t.Run(tt.in, func(t *testing.T) {
37+
endpoint := AWSCognito(tt.in)
38+
if endpoint != tt.out {
39+
t.Errorf("got %q, want %q", endpoint, tt.out)
40+
}
41+
})
42+
}
43+
}

0 commit comments

Comments
 (0)