forked from hidu/api-front
-
Notifications
You must be signed in to change notification settings - Fork 0
/
oauth2.go
139 lines (125 loc) · 3.34 KB
/
oauth2.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
package proxy
import (
"golang.org/x/oauth2"
"github.com/antonholmquist/jason"
"io/ioutil"
"log"
"net/http"
// "net/url"
"fmt"
)
type oauth2Conf struct {
Type string `json:"type"`
Enable bool `json:"enable"`
ClientID string `json:"client_id"`
ClientSecret string `json:"client_sk"`
Scopes []string `json:"scopes"`
AuthURL string `json:"auth_url"`
TokenURL string `json:"token_url"`
Apis map[string]*oauth2Api `json:"apis"`
FieldMap map[string]string `json:"field_map"`
config *oauth2.Config
}
type oauth2Api struct {
ApiUrl string `json:"url"`
FieldMap oauth2ApiFields `json:"field_map"`
}
const OAUTH2_API_USER_INFO = "user_info"
type oauth2ApiFields map[string]string
var oauth2ApiFieldsDefault = map[string]map[string]string{
OAUTH2_API_USER_INFO: {
//标准名字,当前名字
"id": "id",
"nick_name": "name",
"email": "email",
"picture": "picture",
},
}
func (conf *oauth2Conf) getOauthApi(name string) *oauth2Api {
if api, has := conf.Apis[name]; has {
return api
}
log.Fatalln("oauth2 api ", name, " not config")
return nil
}
func (conf *oauth2Conf) getOauthUrl(redirectURL string) string {
config := &oauth2.Config{
ClientID: conf.ClientID,
ClientSecret: conf.ClientSecret,
Scopes: conf.Scopes,
RedirectURL: redirectURL,
Endpoint: oauth2.Endpoint{
AuthURL: conf.AuthURL,
TokenURL: conf.TokenURL,
},
}
urlStr := config.AuthCodeURL("state", oauth2.AccessTypeOffline)
// if(conf.FieldMap!=nil){
// u,_:=url.Parse(urlStr)
// qs:=u.Query()
// for key,keyNow:=range conf.FieldMap{
// qs[keyNow]=qs[key]
// delete(qs,key)
// }
// log.Println("qsqs",qs)
// u.RawQuery=qs.Encode()
// urlStr=u.String()
// }
conf.config = config
return urlStr
}
func (conf *oauth2Conf) checkConf() {
api := conf.getOauthApi(OAUTH2_API_USER_INFO)
if api.FieldMap == nil {
api.FieldMap = oauth2ApiFields{}
}
for k, v := range oauth2ApiFieldsDefault[OAUTH2_API_USER_INFO] {
cv, has := api.FieldMap[k]
if !has || cv == "" {
api.FieldMap[k] = v
}
}
}
func (conf *oauth2Conf) getAccessToken(req *http.Request) (*oauth2.Token, error) {
code := req.FormValue("code")
tok, err := conf.config.Exchange(oauth2.NoContext, code)
if err != nil {
log.Println("Exchange failed,tok:", tok, "error:", err)
return nil, err
}
return tok, err
}
func (conf *oauth2Conf) getUserInfo(tok *oauth2.Token) (*User, error) {
client := conf.config.Client(oauth2.NoContext, tok)
api := conf.getOauthApi(OAUTH2_API_USER_INFO)
resp, err := client.Get(api.ApiUrl)
if err != nil {
log.Println("call api faild,error:", err)
return nil, err
}
data, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
log.Println("getOauthInfo:", string(data))
obj, err := jason.NewObjectFromBytes(data)
if err != nil {
return nil, err
}
log.Println("user_info_obj:", obj)
vs := make(map[string]string)
for key, keyNow := range api.FieldMap {
val, _ := obj.GetString(keyNow)
vs[key] = val
}
if vs["id"] == "" {
return nil, fmt.Errorf("response has no user info:" + string(data))
}
user := &User{
ID: vs["id"],
NickName: vs["nick_name"],
Picture: vs["picture"],
Email: vs["email"],
}
return user, nil
}