Skip to content

armgit / fider

Public Imported from GitHub
0

Open platform to collect and prioritize feedback

fix: support twitch new oauth (#913)

Guilherme Oenning20e2bc376b63
diff --git a/app/services/oauth/custom.go b/app/services/oauth/custom.go
new file mode 100644
index 000000000..079c57fb0
--- /dev/null
+++ b/app/services/oauth/custom.go
@@ -0,0 +1,19 @@
+package oauth
+
+import "net/url"
+
+var providerParams = map[string]map[string]string{
+ "id.twitch.tv": {
+ "claims": `{"userinfo":{"preferred_username":null,"email":null,"email_verified":null}}`,
+ },
+}
+
+func getProviderInitialParams(u *url.URL) url.Values {
+ v := url.Values{}
+ if params, ok := providerParams[u.Hostname()]; ok {
+ for key, value := range params {
+ v.Add(key, value)
+ }
+ }
+ return v
+}
diff --git a/app/services/oauth/oauth.go b/app/services/oauth/oauth.go
index da85f811a..438352cf8 100644
--- a/app/services/oauth/oauth.go
+++ b/app/services/oauth/oauth.go
@@ -60,7 +60,7 @@ func getProviderStatus(key string) int {
var (
systemProviders = []*models.OAuthConfig{
- &models.OAuthConfig{
+ {
Provider: app.FacebookProvider,
DisplayName: "Facebook",
ProfileURL: "https://graph.facebook.com/me?fields=name,email",
@@ -74,7 +74,7 @@ var (
JSONUserNamePath: "name",
JSONUserEmailPath: "email",
},
- &models.OAuthConfig{
+ {
Provider: app.GoogleProvider,
DisplayName: "Google",
ProfileURL: "https://www.googleapis.com/oauth2/v2/userinfo",
@@ -88,7 +88,7 @@ var (
JSONUserNamePath: "name",
JSONUserEmailPath: "email",
},
- &models.OAuthConfig{
+ {
Provider: app.GitHubProvider,
DisplayName: "GitHub",
ProfileURL: "https://api.github.com/user",
@@ -147,12 +147,13 @@ func getOAuthAuthorizationURL(ctx context.Context, q *query.GetOAuthAuthorizatio
oauthBaseURL := web.OAuthBaseURL(ctx)
authURL, _ := url.Parse(config.AuthorizeURL)
- parameters := url.Values{}
+ parameters := getProviderInitialParams(authURL)
parameters.Add("client_id", config.ClientID)
parameters.Add("scope", config.Scope)
parameters.Add("redirect_uri", fmt.Sprintf("%s/oauth/%s/callback", oauthBaseURL, q.Provider))
parameters.Add("response_type", "code")
parameters.Add("state", q.Redirect+"|"+q.Identifier)
+
authURL.RawQuery = parameters.Encode()
q.Result = authURL.String()
return nil
@@ -225,6 +226,8 @@ func getOAuthRawProfile(ctx context.Context, q *query.GetOAuthRawProfile) error
},
}
+ println(oauthToken.AccessToken)
+
if err := bus.Dispatch(ctx, req); err != nil {
return err
}
diff --git a/app/services/oauth/oauth_test.go b/app/services/oauth/oauth_test.go
index 46b82f61f..c39fe12d3 100644
--- a/app/services/oauth/oauth_test.go
+++ b/app/services/oauth/oauth_test.go
@@ -113,6 +113,34 @@ func TestGetAuthURL_Custom(t *testing.T) {
Expect(authURL.Result).Equals("https://example.org/oauth/authorize?client_id=CU_CL_ID&redirect_uri=http%3A%2F%2Flogin.test.fider.io%3A3000%2Foauth%2F_custom%2Fcallback&response_type=code&scope=profile+email&state=http%3A%2F%2Fexample.org%7C456")
}
+func TestGetAuthURL_Twitch(t *testing.T) {
+ RegisterT(t)
+ bus.Init(&oauth.Service{})
+
+ bus.AddHandler(func(ctx context.Context, q *query.GetCustomOAuthConfigByProvider) error {
+ if q.Provider == "_custom" {
+ q.Result = &models.OAuthConfig{
+ Provider: q.Provider,
+ ClientID: "CU_CL_ID",
+ Scope: "openid",
+ AuthorizeURL: "https://id.twitch.tv/oauth/authorize",
+ }
+ }
+ return nil
+ })
+
+ ctx := newGetContext("http://login.test.fider.io:3000")
+ authURL := &query.GetOAuthAuthorizationURL{
+ Provider: "_custom",
+ Redirect: "http://example.org",
+ Identifier: "456",
+ }
+
+ err := bus.Dispatch(ctx, authURL)
+ Expect(err).IsNil()
+ Expect(authURL.Result).Equals("https://id.twitch.tv/oauth/authorize?claims=%7B%22userinfo%22%3A%7B%22preferred_username%22%3Anull%2C%22email%22%3Anull%2C%22email_verified%22%3Anull%7D%7D&client_id=CU_CL_ID&redirect_uri=http%3A%2F%2Flogin.test.fider.io%3A3000%2Foauth%2F_custom%2Fcallback&response_type=code&scope=openid&state=http%3A%2F%2Fexample.org%7C456")
+}
+
func TestParseProfileResponse_AllFields(t *testing.T) {
RegisterT(t)
bus.Init(&oauth.Service{})