-
Notifications
You must be signed in to change notification settings - Fork 2
/
oauth_google.go
93 lines (81 loc) · 2.08 KB
/
oauth_google.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
package oauth
import (
ejson "encoding/json"
"fmt"
"io/ioutil"
"strconv"
"golang.org/x/oauth2"
"golang.org/x/oauth2/google"
"gozen/config"
)
type OAuthGoogle struct {
Id string `json:"id"`
Email string `json:"email"`
VerifiedEmail string `json:"verified_email"`
Name string `json:"name"`
GivenName string `json:"given_name"`
FamilyName string `json:"family_name"`
Link string `json:"link"`
Picture string `json:"picture"`
Gender string `json:"gender"`
Locale string `json:"locale"`
}
// Google OAuth 設定
var googleScopes = []string{
"profile",
"email",
}
var conf = &oauth2.Config{
ClientID: config.Oauth.Google.ClientID,
ClientSecret: config.Oauth.Google.ClientSecret,
RedirectURL: config.Oauth.Google.RedirectURL,
Scopes: googleScopes,
Endpoint: google.Endpoint,
}
func NewOAuthGoogle() User {
return new(OAuthGoogle)
}
// リダイレクトURLを作成する
func (self *OAuthGoogle) GenerateLoginUrl() string {
return conf.AuthCodeURL("state")
}
// CallBack処理を行う
func (self *OAuthGoogle) Callback(state string, code string) (User, error) {
var tok, err = conf.Exchange(oauth2.NoContext, code)
if err != nil {
fmt.Printf("%v", err)
}
var client = conf.Client(oauth2.NoContext, tok)
response, err := client.Get("https://www.googleapis.com/oauth2/v2/userinfo")
defer response.Body.Close()
contents, err := ioutil.ReadAll(response.Body)
ejson.Unmarshal(contents, &self)
return self, err
}
func (self *OAuthGoogle) GetID() *int {
id, _ := strconv.Atoi(self.Id)
return &id
}
func (self *OAuthGoogle) GetName() *string {
return &self.Name
}
func (self *OAuthGoogle) GetEmail() *string {
return &self.Email
}
func (self *OAuthGoogle) GetSource() string {
return "google"
}
func (self *OAuthGoogle) GetClientID() *string {
clientID := config.Oauth.Google.ClientID
if clientID == "" {
return nil
}
return &clientID
}
func (self *OAuthGoogle) GetClientSecret() *string {
clientSecret := config.Oauth.Google.ClientSecret
if clientSecret == "" {
return nil
}
return &clientSecret
}