This repository has been archived by the owner on Dec 7, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 51
/
github_service.go
144 lines (116 loc) · 3.04 KB
/
github_service.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package service
import (
"errors"
"github.com/HackIllinois/api/services/auth/config"
"github.com/HackIllinois/api/services/auth/models"
"github.com/levigross/grequests"
"strconv"
"strings"
)
type GitHubOAuthProvider struct {
token string
isVerifiedUser bool
}
func NewGitHubOAuth() *GitHubOAuthProvider {
return &GitHubOAuthProvider{
token: "",
isVerifiedUser: false,
}
}
/*
Returns the url to redirects to for OAuth authorization
*/
func (provider *GitHubOAuthProvider) GetAuthorizationRedirect(redirect_uri string) (string, error) {
return ConstructSafeURL("https", "github.com", "login/oauth/authorize",
map[string]string{
"client_id": config.GITHUB_CLIENT_ID,
"scope": "user:email",
"redirect_uri": redirect_uri,
},
)
}
/*
Exchanges an OAuth code for an OAuth token
*/
func (provider *GitHubOAuthProvider) Authorize(code string, redirect_uri string) error {
request, err := grequests.Post("https://github.com/login/oauth/access_token", &grequests.RequestOptions{
Params: map[string]string{
"client_id": config.GITHUB_CLIENT_ID,
"client_secret": config.GITHUB_CLIENT_SECRET,
"code": code,
},
Headers: map[string]string{
"Accept": "application/json",
},
})
if err != nil {
return err
}
var oauth_token models.GithubOauthToken
err = request.JSON(&oauth_token)
if err != nil {
return err
}
if oauth_token.Token == "" {
return errors.New("Invalid oauth code")
}
provider.token = oauth_token.Token
return nil
}
/*
Retrieves user info from the OAuth provider
*/
func (provider *GitHubOAuthProvider) GetUserInfo() (*models.UserInfo, error) {
var user_info models.UserInfo
request, err := grequests.Get("https://api.github.com/user", &grequests.RequestOptions{
Headers: map[string]string{
"Authorization": "token " + provider.token,
},
})
if err != nil {
return nil, err
}
var github_user_info models.GithubUserInfo
err = request.JSON(&github_user_info)
if err != nil {
return nil, err
}
if github_user_info.ID == 0 {
return nil, errors.New("Invalid oauth token")
}
user_info.Username = github_user_info.Username
user_info.ID = "github" + strconv.Itoa(github_user_info.ID)
split_name := strings.SplitAfterN(github_user_info.Name, " ", 2)
user_info.FirstName = strings.TrimSpace(split_name[0])
if len(split_name) < 2 {
user_info.LastName = ""
} else {
user_info.LastName = strings.TrimSpace(split_name[1])
}
request, err = grequests.Get("https://api.github.com/user/emails", &grequests.RequestOptions{
Headers: map[string]string{
"Authorization": "token " + provider.token,
},
})
if err != nil {
return nil, err
}
var emails []models.GithubEmail
err = request.JSON(&emails)
if err != nil {
return nil, err
}
for _, email := range emails {
if email.IsPrimary {
user_info.Email = email.Email
provider.isVerifiedUser = email.IsVerified
}
}
return &user_info, nil
}
/*
Returns true if the user has a verified email
*/
func (provider *GitHubOAuthProvider) IsVerifiedUser() bool {
return provider.isVerifiedUser
}