This repository has been archived by the owner on Jun 27, 2023. It is now read-only.
/
github.go
60 lines (49 loc) · 1.41 KB
/
github.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package drivers
import (
"encoding/json"
"net/http"
"github.com/danilopolani/gocialite/structs"
"golang.org/x/oauth2/github"
)
const githubDriverName = "github"
func init() {
registerDriver(githubDriverName, GithubDefaultScopes, GithubUserFn, github.Endpoint, GithubAPIMap, GithubUserMap)
}
// GithubUserMap is the map to create the User struct
var GithubUserMap = map[string]string{
"id": "ID",
"email": "Email",
"login": "Username",
"avatar_url": "Avatar",
"name": "FullName",
}
// GithubAPIMap is the map for API endpoints
var GithubAPIMap = map[string]string{
"endpoint": "https://api.github.com",
"userEndpoint": "/user",
"emailEndpoint": "/user/emails",
}
// GithubUserFn is a callback to parse additional fields for User
var GithubUserFn = func(client *http.Client, u *structs.User) {
// Used to parse the email from response
type additionalEmail struct {
Email string `json:"email"`
}
var email []additionalEmail
// Email can be nil because of the "keep my email private" setting
if u.Email == "<nil>" {
// Retrieve email
req, err := client.Get(GithubAPIMap["endpoint"] + GithubAPIMap["emailEndpoint"])
if err != nil {
return
}
defer req.Body.Close()
err = json.NewDecoder(req.Body).Decode(&email)
if err != nil {
return
}
u.Email = email[0].Email
}
}
// GithubDefaultScopes contains the default scopes
var GithubDefaultScopes = []string{"user:email"}