diff --git a/README.md b/README.md index 83ed57f..9e48ae4 100644 --- a/README.md +++ b/README.md @@ -605,7 +605,7 @@ For more details refer to [Complete Guide of Battle.net OAuth API and Login Butt 1. Take note of the **Client ID** and **Client Secret** #### Discord Auth Provider #### -1. Log into Discord Developer Portal https://discord.com/developers/applications +1. Log into Discord Developer Portal https://discord.com/developers/applications 2. Click on **New Application** to create the application required for Oauth 3. After filling **"NAME"**, navigate to **"OAuth2"** option on the left sidebar 4. Under **"Redirects"** enter the correct url constructed as domain + `/auth/discord/callback`. ie `https://remark42.mysite.com/auth/discord/callback` diff --git a/provider/providers.go b/provider/providers.go index 32b53aa..9711781 100644 --- a/provider/providers.go +++ b/provider/providers.go @@ -276,16 +276,13 @@ func NewDiscord(p Params) Oauth2Handler { TokenURL: "https://discord.com/api/oauth2/token", }, infoURL: "https://discord.com/api/v10/users/@me", - scopes: []string{"email", "identify"}, + scopes: []string{"identify"}, mapUser: func(data UserData, _ []byte) token.User { userInfo := token.User{ ID: "discord_" + token.HashID(sha1.New(), data.Value("id")), Name: data.Value("username"), Picture: fmt.Sprintf("https://cdn.discordapp.com/avatars/%s/%s.webp", data.Value("id"), data.Value("avatar")), } - if data.Value("email") != "" { - userInfo.Email = data.Value("email") - } for k, v := range p.UserAttributes { userInfo.SetStrAttr(v, data.Value(k)) diff --git a/v2/auth.go b/v2/auth.go index 30d4a61..9077b2d 100644 --- a/v2/auth.go +++ b/v2/auth.go @@ -257,6 +257,8 @@ func (s *Service) addProviderByName(name string, p provider.Params) { prov = provider.NewTwitter(p) case "patreon": prov = provider.NewPatreon(p) + case "discord": + prov = provider.NewDiscord(p) case "dev": prov = provider.NewDev(p) default: diff --git a/v2/auth_test.go b/v2/auth_test.go index 0031d55..3e5ddbb 100644 --- a/v2/auth_test.go +++ b/v2/auth_test.go @@ -61,6 +61,7 @@ func TestProvider(t *testing.T) { svc.AddProvider("twitter", "cid", "csecret") svc.AddProvider("battlenet", "cid", "csecret") svc.AddProvider("patreon", "cid", "csecret") + svc.AddProvider("discord", "cid", "csecret") svc.AddProvider("bad", "cid", "csecret") c := customHandler{} @@ -81,7 +82,7 @@ func TestProvider(t *testing.T) { assert.Equal(t, "github", op.Name()) pp := svc.Providers() - assert.Equal(t, 10, len(pp)) + assert.Equal(t, 11, len(pp)) ch, err := svc.Provider("telegramBotMySiteCom") assert.NoError(t, err) diff --git a/v2/provider/providers.go b/v2/provider/providers.go index 5f8758e..471fa31 100644 --- a/v2/provider/providers.go +++ b/v2/provider/providers.go @@ -265,3 +265,29 @@ func NewPatreon(p Params) Oauth2Handler { }, }) } + +// NewDiscord makes discord oauth2 provider +func NewDiscord(p Params) Oauth2Handler { + return initOauth2Handler(p, Oauth2Handler{ + name: "discord", + // see https://discord.com/developers/docs/topics/oauth2 + endpoint: oauth2.Endpoint{ + AuthURL: "https://discord.com/oauth2/authorize", + TokenURL: "https://discord.com/api/oauth2/token", + }, + infoURL: "https://discord.com/api/v10/users/@me", + scopes: []string{"identify"}, + mapUser: func(data UserData, _ []byte) token.User { + userInfo := token.User{ + ID: "discord_" + token.HashID(sha1.New(), data.Value("id")), + Name: data.Value("username"), + Picture: fmt.Sprintf("https://cdn.discordapp.com/avatars/%s/%s.webp", data.Value("id"), data.Value("avatar")), + } + + for k, v := range p.UserAttributes { + userInfo.SetStrAttr(v, data.Value(k)) + } + return userInfo + }, + }) +} diff --git a/v2/provider/providers_test.go b/v2/provider/providers_test.go index 7d3b8fe..cfdc0b6 100644 --- a/v2/provider/providers_test.go +++ b/v2/provider/providers_test.go @@ -208,3 +208,16 @@ func TestProviders_NewPatreon(t *testing.T) { user, ) } + +func TestProviders_NewDiscord(t *testing.T) { + r := NewDiscord(Params{URL: "http://demo.remark42.com", Cid: "cid", Csecret: "cs"}) + assert.Equal(t, "discord", r.Name()) + + t.Run("With all data", func(t *testing.T) { + udata := UserData{"id": "248533295981532", "username": "test_user", "avatar": "374384984773", "email": "test@email.com"} + user := r.mapUser(udata, nil) + assert.Equal(t, token.User{Name: "test_user", ID: "discord_9b472605c1318483fb4b88f9acf22cdd4219f9a0", Email: "test@email.com", + Picture: "https://cdn.discordapp.com/avatars/248533295981532/374384984773.webp"}, user, "got %+v", user) + }) + +}