-
Notifications
You must be signed in to change notification settings - Fork 150
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds support for Discord as an OAuth provider. Fetches user info and also the guilds (servers) that user is a member of, and returns the IDs of said servers as the Groups userinfo attribute. This allows, in conjunction with userfile, to authenticate users based on their membership of discord servers.
- Loading branch information
Showing
4 changed files
with
191 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
package oauth2 | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"io/ioutil" | ||
"net/http" | ||
"strings" | ||
|
||
"github.com/tarent/loginsrv/model" | ||
) | ||
|
||
var discordAPI = "https://discordapp.com/api" | ||
var discordCDN = "https://cdn.discordapp.com" | ||
|
||
func init() { | ||
RegisterProvider(providerDiscord) | ||
} | ||
|
||
// DiscordUser is used for parsing the github response | ||
type DiscordUser struct { | ||
ID string `json:"id,omitempty"` | ||
Username string `json:"username,omitempty"` | ||
Discriminator string `json:"discriminator,omitempty"` | ||
AvatarHash string `json:"avatar,omitempty"` | ||
MFAEnabled bool `json:"mfa_enabled,omitempty"` | ||
Locale string `json:"locale,omitempty"` | ||
Verified bool `json:"verified,omitempty"` | ||
Email string `json:"email,omitempty"` | ||
Flags int `json:"flags,omitempty"` | ||
PremiumType int `json:"premium_type,omitempty"` | ||
} | ||
|
||
// DiscordGuild is a partial guild object returned by the /user/guilds endpoint | ||
type DiscordGuild struct { | ||
ID string `json:"id,omitempty"` | ||
Name string `json:"name,omitempty"` | ||
IconHash string `json:"icon,omitempty"` | ||
Owner bool `json:"owner,omitempty"` | ||
Permissions int `json:"permissions,omitempty"` | ||
} | ||
|
||
func discordAPIRequest(endpoint, token string) ([]byte, error) { | ||
req, err := http.NewRequest("GET", fmt.Sprintf("%v/%v", discordAPI, endpoint), nil) | ||
if err != nil { | ||
return nil, fmt.Errorf("create request: %v", err) | ||
} | ||
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", token)) | ||
|
||
resp, err := http.DefaultClient.Do(req) | ||
if err != nil { | ||
return nil, err | ||
} | ||
defer resp.Body.Close() | ||
|
||
if !strings.Contains(resp.Header.Get("Content-Type"), "application/json") { | ||
return nil, fmt.Errorf("wrong content-type: %v", resp.Header.Get("Content-Type")) | ||
} | ||
|
||
if resp.StatusCode != 200 { | ||
return nil, fmt.Errorf("got http status %v", resp.StatusCode) | ||
} | ||
|
||
b, err := ioutil.ReadAll(resp.Body) | ||
if err != nil { | ||
return nil, fmt.Errorf("error reading body: %v", err) | ||
} | ||
|
||
return b, nil | ||
} | ||
|
||
var providerDiscord = Provider{ | ||
Name: "discord", | ||
AuthURL: "https://discordapp.com/api/oauth2/authorize?prompt=none", | ||
TokenURL: "https://discordapp.com/api/oauth2/token", | ||
DefaultScopes: "identify email guilds", | ||
GetUserInfo: func(token TokenInfo) (model.UserInfo, string, error) { | ||
du := DiscordUser{} | ||
dg := []DiscordGuild{} | ||
// Get user info | ||
raw, err := discordAPIRequest("/users/@me", token.AccessToken) | ||
if err != nil { | ||
return model.UserInfo{}, "", fmt.Errorf("error getting discord user info: %v", err) | ||
} | ||
err = json.Unmarshal(raw, &du) | ||
if err != nil { | ||
return model.UserInfo{}, "", fmt.Errorf("error parsing discord get user info: %v", err) | ||
} | ||
|
||
// Get user's guilds (servers) | ||
raw, err = discordAPIRequest("/users/@me/guilds", token.AccessToken) | ||
if err != nil { | ||
return model.UserInfo{}, "", fmt.Errorf("error getting discord user guilds: %v", err) | ||
} | ||
err = json.Unmarshal(raw, &dg) | ||
if err != nil { | ||
return model.UserInfo{}, "", fmt.Errorf("error parsing discord guilds: %v", err) | ||
} | ||
|
||
var guilds []string | ||
for _, g := range dg { | ||
guilds = append(guilds, g.ID) | ||
} | ||
|
||
return model.UserInfo{ | ||
Sub: fmt.Sprintf("%v#%v", du.Username, du.Discriminator), | ||
Picture: fmt.Sprintf("%v/avatars/%v/%v.png", discordCDN, du.ID, du.AvatarHash), | ||
Name: du.Username, | ||
Email: du.Email, | ||
Origin: "discord", | ||
Groups: guilds, | ||
}, string(raw), nil | ||
}, | ||
} |
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 |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package oauth2 | ||
|
||
import ( | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
|
||
"github.com/gorilla/mux" | ||
. "github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/suite" | ||
) | ||
|
||
var discordTestUserResponse = `{ | ||
"id": "80351110224678912", | ||
"username": "Nelly", | ||
"discriminator": "1337", | ||
"avatar": "8342729096ea3675442027381ff50dfe", | ||
"verified": true, | ||
"email": "nelly@discordapp.com", | ||
"flags": 64, | ||
"premium_type": 1 | ||
}` | ||
|
||
var discordTestUserGuildsResponse = `[ | ||
{ | ||
"id": "80351110224678912", | ||
"name": "1337 Krew", | ||
"icon": "8342729096ea3675442027381ff50dfe", | ||
"owner": true, | ||
"permissions": 36953089 | ||
} | ||
]` | ||
|
||
type DiscordTestSuite struct { | ||
suite.Suite | ||
Server *httptest.Server | ||
} | ||
|
||
func (suite *DiscordTestSuite) SetupTest() { | ||
r := mux.NewRouter() | ||
|
||
r.HandleFunc("/users/@me", func(w http.ResponseWriter, r *http.Request) { | ||
suite.Equal(r.Header.Get("Authentication"), "Bearer secret") | ||
w.Header().Set("Content-Type", "application/json; charset=utf-8") | ||
w.Write([]byte(discordTestUserResponse)) | ||
}) | ||
|
||
r.HandleFunc("/users/@me/guilds", func(w http.ResponseWriter, r *http.Request) { | ||
suite.Equal(r.Header.Get("Authentication"), "Bearer secret") | ||
w.Header().Set("Content-Type", "application/json; charset=utf-8") | ||
w.Write([]byte(discordTestUserGuildsResponse)) | ||
}) | ||
|
||
suite.Server = httptest.NewServer(r) | ||
} | ||
|
||
func (suite *DiscordTestSuite) Test_Discord_getUserInfo(t *testing.T) { | ||
discordAPI = suite.Server.URL | ||
|
||
u, rawJSON, err := providerDiscord.GetUserInfo(TokenInfo{AccessToken: "secret"}) | ||
NoError(t, err) | ||
Equal(t, "Nelly#1337", u.Sub) | ||
Equal(t, "nelly@discordapp.com", u.Email) | ||
Equal(t, "Nelly", u.Name) | ||
Equal(t, []string{"80351110224678912"}, u.Groups) | ||
Equal(t, discordTestUserResponse, rawJSON) | ||
} |
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