-
Notifications
You must be signed in to change notification settings - Fork 0
/
oauth.go
111 lines (97 loc) · 2.61 KB
/
oauth.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
package unionPayApp
import (
"encoding/base64"
"github.com/forgoer/openssl"
"github.com/go-tron/crypto/desUtil"
"github.com/google/go-querystring/query"
)
type Scope string
const (
ScopeBase Scope = "upapi_base"
ScopeMobile Scope = "upapi_mobile"
)
type OAuthCodeReq struct {
Uri string `json:"uri"`
Scope Scope `json:"scope"`
State string `json:"state"`
}
type OAuthCodeQuery struct {
AppId string `url:"appId"`
RedirectUri string `url:"redirectUri"`
ResponseType string `url:"responseType"`
Scope string `url:"scope"`
State string `url:"state"`
}
func (upa *UnionPayApp) GetOAuthCode(params *OAuthCodeReq) (string, error) {
req := OAuthCodeQuery{
AppId: upa.AppId,
RedirectUri: params.Uri,
ResponseType: "code",
Scope: string(params.Scope),
State: params.State,
}
if upa.OAuthRedirectUri != "" {
req.RedirectUri = upa.OAuthRedirectUri + req.RedirectUri
}
v, err := query.Values(req)
if err != nil {
return "", err
}
return "https://open.95516.com/s/open/html/oauth.html?" + v.Encode(), nil
}
type OAuthToken struct {
AccessToken string `json:"accessToken"`
ExpiresIn int64 `json:"expiresIn"`
RefreshToken string `json:"refreshToken"`
OpenId string `json:"openId"`
Scope string `json:"scope"`
}
func (upa *UnionPayApp) GetOAuthToken(code string) (*OAuthToken, error) {
res, err := upa.Request("OAuthToken", map[string]interface{}{
"appId": upa.AppId,
"code": code,
"grantType": "authorization_code",
}, &OAuthToken{})
if err != nil {
return nil, err
}
return res.(*OAuthToken), nil
}
type OAuthMobileReq struct {
OpenId string `json:"openId"`
AccessToken string `json:"accessToken"`
}
type OAuthMobile struct {
Mobile string `json:"mobile"`
}
func (upa *UnionPayApp) GetOAuthMobile(params *OAuthMobileReq) (*OAuthMobile, error) {
res, err := upa.Request("OAuthMobile", map[string]interface{}{
"appId": upa.AppId,
"accessToken": params.AccessToken,
"openId": params.OpenId,
}, &OAuthMobile{})
if err != nil {
return nil, err
}
result := res.(*OAuthMobile)
src, err := base64.StdEncoding.DecodeString(result.Mobile)
if err != nil {
return nil, err
}
mobile, err := desUtil.Des3ECBDecrypt(src, upa.encryptKeyByte, openssl.PKCS5_PADDING)
if err != nil {
return nil, err
}
result.Mobile = string(mobile)
return result, nil
}
func (upa *UnionPayApp) GetOAuthMobileFromCode(code string) (*OAuthMobile, error) {
res, err := upa.GetOAuthToken(code)
if err != nil {
return nil, err
}
return upa.GetOAuthMobile(&OAuthMobileReq{
OpenId: res.OpenId,
AccessToken: res.AccessToken,
})
}