-
Notifications
You must be signed in to change notification settings - Fork 940
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(thirdparty): add discord provider
Co-authored-by: Scott Plunkett <[email protected]>
- Loading branch information
1 parent
cfd11cd
commit 7d57bf6
Showing
14 changed files
with
361 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,12 +2,13 @@ package handler | |
|
||
import ( | ||
"fmt" | ||
"github.com/h2non/gock" | ||
"github.com/teamhanko/hanko/backend/thirdparty" | ||
"github.com/teamhanko/hanko/backend/utils" | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
|
||
"github.com/h2non/gock" | ||
"github.com/teamhanko/hanko/backend/thirdparty" | ||
"github.com/teamhanko/hanko/backend/utils" | ||
) | ||
|
||
func (s *thirdPartySuite) TestThirdPartyHandler_Callback_SignUp_Google() { | ||
|
@@ -383,6 +384,125 @@ func (s *thirdPartySuite) TestThirdPartyHandler_Callback_SignIn_Apple() { | |
} | ||
} | ||
|
||
func (s *thirdPartySuite) TestThirdPartyHandler_Callback_SignUp_Discord() { | ||
defer gock.Off() | ||
if testing.Short() { | ||
s.T().Skip("skipping test in short mode.") | ||
} | ||
|
||
gock.New(thirdparty.DiscordOauthTokenEndpoint). | ||
Post("/"). | ||
Reply(200). | ||
JSON(map[string]string{"access_token": "fakeAccessToken"}) | ||
|
||
gock.New(thirdparty.DiscordUserInfoEndpoint). | ||
Get("/"). | ||
Reply(200). | ||
JSON(&thirdparty.DiscordUser{ | ||
ID: "discord_abcde", | ||
Email: "[email protected]", | ||
Verified: true, | ||
}) | ||
|
||
cfg := s.setUpConfig([]string{"discord"}, []string{"https://example.com"}) | ||
|
||
state, err := thirdparty.GenerateState(cfg, "discord", "https://example.com") | ||
s.NoError(err) | ||
|
||
req := httptest.NewRequest(http.MethodGet, fmt.Sprintf("/thirdparty/callback?code=abcde&state=%s", state), nil) | ||
req.AddCookie(&http.Cookie{ | ||
Name: utils.HankoThirdpartyStateCookie, | ||
Value: string(state), | ||
}) | ||
|
||
c, rec := s.setUpContext(req) | ||
handler := s.setUpHandler(cfg) | ||
|
||
if s.NoError(handler.Callback(c)) { | ||
s.Equal(http.StatusTemporaryRedirect, rec.Code) | ||
|
||
s.assertLocationHeaderHasToken(rec) | ||
s.assertStateCookieRemoved(rec) | ||
|
||
email, err := s.Storage.GetEmailPersister().FindByAddress("[email protected]") | ||
s.NoError(err) | ||
s.NotNil(email) | ||
s.True(email.IsPrimary()) | ||
|
||
user, err := s.Storage.GetUserPersister().Get(*email.UserID) | ||
s.NoError(err) | ||
s.NotNil(user) | ||
|
||
identity := email.Identities.GetIdentity("discord", "discord_abcde") | ||
s.NotNil(identity) | ||
|
||
logs, lerr := s.Storage.GetAuditLogPersister().List(0, 0, nil, nil, []string{"thirdparty_signup_succeeded"}, user.ID.String(), email.Address, "", "") | ||
s.NoError(lerr) | ||
s.Len(logs, 1) | ||
} | ||
} | ||
|
||
func (s *thirdPartySuite) TestThirdPartyHandler_Callback_SignIn_Discord() { | ||
defer gock.Off() | ||
if testing.Short() { | ||
s.T().Skip("skipping test in short mode.") | ||
} | ||
|
||
err := s.LoadFixtures("../test/fixtures/thirdparty") | ||
s.NoError(err) | ||
|
||
gock.New(thirdparty.DiscordOauthTokenEndpoint). | ||
Post("/"). | ||
Reply(200). | ||
JSON(map[string]string{"access_token": "fakeAccessToken"}) | ||
|
||
gock.New(thirdparty.DiscordUserInfoEndpoint). | ||
Get("/"). | ||
Reply(200). | ||
JSON(&thirdparty.DiscordUser{ | ||
ID: "discord_abcde", | ||
Email: "[email protected]", | ||
Verified: true, | ||
}) | ||
|
||
cfg := s.setUpConfig([]string{"discord"}, []string{"https://example.com"}) | ||
|
||
state, err := thirdparty.GenerateState(cfg, "discord", "https://example.com") | ||
s.NoError(err) | ||
|
||
req := httptest.NewRequest(http.MethodGet, fmt.Sprintf("/thirdparty/callback?code=abcde&state=%s", state), nil) | ||
req.AddCookie(&http.Cookie{ | ||
Name: utils.HankoThirdpartyStateCookie, | ||
Value: string(state), | ||
}) | ||
|
||
c, rec := s.setUpContext(req) | ||
handler := s.setUpHandler(cfg) | ||
|
||
if s.NoError(handler.Callback(c)) { | ||
s.Equal(http.StatusTemporaryRedirect, rec.Code) | ||
|
||
s.assertLocationHeaderHasToken(rec) | ||
s.assertStateCookieRemoved(rec) | ||
|
||
email, err := s.Storage.GetEmailPersister().FindByAddress("[email protected]") | ||
s.NoError(err) | ||
s.NotNil(email) | ||
s.True(email.IsPrimary()) | ||
|
||
user, err := s.Storage.GetUserPersister().Get(*email.UserID) | ||
s.NoError(err) | ||
s.NotNil(user) | ||
|
||
identity := email.Identities.GetIdentity("discord", "discord_abcde") | ||
s.NotNil(identity) | ||
|
||
logs, lerr := s.Storage.GetAuditLogPersister().List(0, 0, nil, nil, []string{"thirdparty_signin_succeeded"}, user.ID.String(), "", "", "") | ||
s.NoError(lerr) | ||
s.Len(logs, 1) | ||
} | ||
} | ||
|
||
func (s *thirdPartySuite) TestThirdPartyHandler_Callback_SignUp_WithUnclaimedEmail() { | ||
defer gock.Off() | ||
if testing.Short() { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,6 +24,12 @@ | |
verified: true | ||
created_at: 2020-12-31 23:59:59 | ||
updated_at: 2020-12-31 23:59:59 | ||
- id: 09f35529-cca6-44a7-ab1d-b07e95a04e3b | ||
user_id: d69bffda-4e4a-4424-a238-fbecc1651d81 | ||
address: [email protected] | ||
verified: true | ||
created_at: 2020-12-31 23:59:59 | ||
updated_at: 2020-12-31 23:59:59 | ||
- id: 527afce8-3b7b-41b6-b1ed-33d408c5a7bb | ||
user_id: 43fb7e88-4d5d-4b2b-9335-391e78d7e472 | ||
address: [email protected] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,3 +19,10 @@ | |
email_id: 05ab6e1f-8dfb-4329-ae04-22571a68d96b | ||
created_at: 2020-12-31 23:59:59 | ||
updated_at: 2020-12-31 23:59:59 | ||
- id: 18d61f13-2789-467a-a3a6-4292c0621580 | ||
provider_id: "discord_abcde" | ||
provider_name: "discord" | ||
data: '{"email":"[email protected]","email_verified":true,"iss":"https://discord.com/api","sub":"discord_abcde"}' | ||
email_id: 09f35529-cca6-44a7-ab1d-b07e95a04e3b | ||
created_at: 2020-12-31 23:59:59 | ||
updated_at: 2020-12-31 23:59:59 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.