Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add thirdparty Discord provider (OAuth 2.0) #1279

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions backend/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ package config
import (
"errors"
"fmt"
"log"
"strings"
"time"

"github.com/fatih/structs"
"github.com/gobwas/glob"
"github.com/kelseyhightower/envconfig"
Expand All @@ -11,9 +15,6 @@ import (
"github.com/knadh/koanf/providers/file"
"github.com/teamhanko/hanko/backend/ee/saml/config"
"golang.org/x/exp/slices"
"log"
"strings"
"time"
)

// Config is the central configuration type
Expand Down Expand Up @@ -582,9 +583,10 @@ func (p *ThirdPartyProvider) Validate() error {
}

type ThirdPartyProviders struct {
Google ThirdPartyProvider `yaml:"google" json:"google,omitempty" koanf:"google"`
GitHub ThirdPartyProvider `yaml:"github" json:"github,omitempty" koanf:"github"`
Apple ThirdPartyProvider `yaml:"apple" json:"apple,omitempty" koanf:"apple"`
Google ThirdPartyProvider `yaml:"google" json:"google,omitempty" koanf:"google"`
GitHub ThirdPartyProvider `yaml:"github" json:"github,omitempty" koanf:"github"`
Apple ThirdPartyProvider `yaml:"apple" json:"apple,omitempty" koanf:"apple"`
Discord ThirdPartyProvider `yaml:"discord" json:"discord,omitempty" koanf:"discord"`
}

func (p *ThirdPartyProviders) Validate() error {
Expand Down
28 changes: 28 additions & 0 deletions backend/docs/Config.md
Original file line number Diff line number Diff line change
Expand Up @@ -562,6 +562,34 @@ third_party:
# Required if provider is enabled.
#
secret: "CHANGE_ME"
##
#
# The Discord provider configuration
#
discord:
##
#
# Enable or disable the Discord provider.
#
# Default: false
#
enabled: false
##
#
# The client ID of your Discord OAuth credentials.
# See: https://docs.hanko.io/guides/authentication-methods/oauth/discord
#
# Required if provider is enabled.
#
client_id: "CHANGE_ME"
##
#
# The secret of your Discord OAuth credentials.
# See: https://docs.hanko.io/guides/authentication-methods/oauth/discord
#
# Required if provider is enabled.
#
secret: "CHANGE_ME"
log:
## log_health_and_metrics
#
Expand Down
9 changes: 6 additions & 3 deletions backend/thirdparty/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@ import (
"encoding/json"
"errors"
"fmt"
"github.com/fatih/structs"
"github.com/teamhanko/hanko/backend/config"
"golang.org/x/oauth2"
"io"
"net/http"
"strings"
"time"

"github.com/fatih/structs"
"github.com/teamhanko/hanko/backend/config"
"golang.org/x/oauth2"
)

type UserData struct {
Expand Down Expand Up @@ -85,6 +86,8 @@ func GetProvider(config config.ThirdParty, name string) (OAuthProvider, error) {
return NewGithubProvider(config.Providers.GitHub, config.RedirectURL)
case "apple":
return NewAppleProvider(config.Providers.Apple, config.RedirectURL)
case "discord":
return NewDiscordProvider(config.Providers.Discord, config.RedirectURL)
default:
return nil, fmt.Errorf("provider '%s' is not supported", name)
}
Expand Down
103 changes: 103 additions & 0 deletions backend/thirdparty/provider_discord.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
package thirdparty

import (
"context"
"errors"
"fmt"

"github.com/teamhanko/hanko/backend/config"
"golang.org/x/oauth2"
)

const (
DiscordAPIBase = "https://discord.com/api"
DiscordOauthAuthEndpoint = "https://discord.com/oauth2/authorize"
DiscordOauthTokenEndpoint = DiscordAPIBase + "/oauth2/token"
DiscordUserInfoEndpoint = DiscordAPIBase + "/users/@me"
)

var DefaultDiscordScopes = []string{
"identify",
"email",
}

type discordProvider struct {
*oauth2.Config
}

type DiscordUser struct {
ID string `json:"id"`
Username string `json:"username"`
GlobalName string `json:"global_name"`
Avatar string `json:"avatar"`
Email string `json:"email"`
Verified bool `json:"verified"`
}

// NewDiscordProvider creates a Discord third party provider.
func NewDiscordProvider(config config.ThirdPartyProvider, redirectURL string) (OAuthProvider, error) {
if !config.Enabled {
return nil, errors.New("discord provider is disabled")
}

return &discordProvider{
Config: &oauth2.Config{
ClientID: config.ClientID,
ClientSecret: config.Secret,
Endpoint: oauth2.Endpoint{
AuthURL: DiscordOauthAuthEndpoint,
TokenURL: DiscordOauthTokenEndpoint,
},
Scopes: DefaultDiscordScopes,
RedirectURL: redirectURL,
},
}, nil
}

func (g discordProvider) GetOAuthToken(code string) (*oauth2.Token, error) {
return g.Exchange(context.Background(), code)
}

func (g discordProvider) GetUserData(token *oauth2.Token) (*UserData, error) {
var user DiscordUser
if err := makeRequest(token, g.Config, DiscordUserInfoEndpoint, &user); err != nil {
return nil, err
}

data := &UserData{}

if user.Email != "" {
data.Emails = append(data.Emails, Email{
Email: user.Email,
Verified: user.Verified,
Primary: true,
})
}

if len(data.Emails) <= 0 {
return nil, errors.New("unable to find email with Discord provider")
}

data.Metadata = &Claims{
Issuer: DiscordAPIBase,
Subject: user.ID,
Name: user.GlobalName,
Picture: g.buildAvatarURL(user.ID, user.Avatar),
Email: user.Email,
EmailVerified: user.Verified,
}

return data, nil
}

func (g discordProvider) buildAvatarURL(userID string, avatarHash string) string {
if avatarHash == "" {
return "" // No image
}

return fmt.Sprintf("https://cdn.discordapp.com/avatars/%s/%s.png", userID, avatarHash)
}

func (g discordProvider) Name() string {
return "discord"
}
25 changes: 25 additions & 0 deletions frontend/elements/src/components/icons/Discord.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { IconProps } from "./Icon";
import cx from "classnames";
import styles from "./styles.sass";

const Discord = ({ size, secondary, disabled }: IconProps) => {
return (
<svg
id="icon-discord"
fill="#fff"
xmlns="http://www.w3.org/2000/svg"
width={size}
height={size}
viewBox="0 0 127.14 96.36"
className={cx(
styles.icon,
secondary && styles.secondary,
disabled && styles.disabled
)}
>
<path d="M107.7,8.07A105.15,105.15,0,0,0,81.47,0a72.06,72.06,0,0,0-3.36,6.83A97.68,97.68,0,0,0,49,6.83,72.37,72.37,0,0,0,45.64,0,105.89,105.89,0,0,0,19.39,8.09C2.79,32.65-1.71,56.6.54,80.21h0A105.73,105.73,0,0,0,32.71,96.36,77.7,77.7,0,0,0,39.6,85.25a68.42,68.42,0,0,1-10.85-5.18c.91-.66,1.8-1.34,2.66-2a75.57,75.57,0,0,0,64.32,0c.87.71,1.76,1.39,2.66,2a68.68,68.68,0,0,1-10.87,5.19,77,77,0,0,0,6.89,11.1A105.25,105.25,0,0,0,126.6,80.22h0C129.24,52.84,122.09,29.11,107.7,8.07ZM42.45,65.69C36.18,65.69,31,60,31,53s5-12.74,11.43-12.74S54,46,53.89,53,48.84,65.69,42.45,65.69Zm42.24,0C78.41,65.69,73.25,60,73.25,53s5-12.74,11.44-12.74S96.23,46,96.12,53,91.08,65.69,84.69,65.69Z"/>
</svg>
);
};

export default Discord;
3 changes: 2 additions & 1 deletion frontend/elements/src/components/icons/icons.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ import { default as exclamation } from "./ExclamationMark";
import { default as google } from "./Google";
import { default as github } from "./GitHub";
import { default as apple } from "./Apple";
import { default as discord } from "./Discord";

export { passkey, spinner, checkmark, exclamation, google, github, apple };
export { passkey, spinner, checkmark, exclamation, google, github, apple, discord };
Loading