-
-
Notifications
You must be signed in to change notification settings - Fork 199
/
oauthApp.go
168 lines (149 loc) · 3.76 KB
/
oauthApp.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package model
import (
"strings"
"github.com/webx-top/com"
"github.com/webx-top/db"
"github.com/webx-top/echo"
stdCode "github.com/webx-top/echo/code"
"github.com/admpub/nging/v5/application/dbschema"
"github.com/admpub/nging/v5/application/library/common"
)
func NewOAuthApp(ctx echo.Context) *OAuthApp {
m := &OAuthApp{
NgingOauthApp: dbschema.NewNgingOauthApp(ctx),
}
return m
}
type OAuthApp struct {
*dbschema.NgingOauthApp
}
func (f *OAuthApp) check() error {
return nil
}
func (f *OAuthApp) Add() (pk interface{}, err error) {
err = f.Context().Begin()
if err != nil {
return
}
defer func() {
f.Context().End(err == nil)
}()
err = f.check()
if err != nil {
return
}
f.NgingOauthApp.AppId, err = f.GenerateAppID()
if err != nil {
return
}
f.NgingOauthApp.AppSecret = f.GenerateAppSecret()
pk, err = f.NgingOauthApp.Insert()
return
}
func (f *OAuthApp) GenerateAppID() (string, error) {
return common.UniqueID()
}
func (f *OAuthApp) GenerateAppSecret() string {
return com.RandomAlphanumeric(32)
}
func (f *OAuthApp) Edit(mw func(db.Result) db.Result, args ...interface{}) (err error) {
err = f.Context().Begin()
if err != nil {
return
}
defer func() {
f.Context().End(err == nil)
}()
err = f.check()
if err != nil {
return
}
if len(f.NgingOauthApp.AppId) == 0 {
f.NgingOauthApp.AppId, err = f.GenerateAppID()
if err != nil {
return
}
}
if len(f.NgingOauthApp.AppSecret) == 0 {
f.NgingOauthApp.AppSecret = f.GenerateAppSecret()
}
err = f.NgingOauthApp.Update(mw, args...)
return
}
func (f *OAuthApp) Exists(appID string) error {
exists, err := f.NgingOauthApp.Exists(nil, db.Cond{`app_id`: appID})
if err != nil {
return err
}
if exists {
err = f.Context().E(`AppID“%s”已经存在`, appID)
}
return err
}
func (f *OAuthApp) MatchDomain(domain string) bool {
return MatchDomain(domain, f.NgingOauthApp)
}
func (f *OAuthApp) MatchScope(scopes ...string) bool {
return len(InvalidScope(scopes, f.NgingOauthApp)) == 0
}
func (f *OAuthApp) GetAndVerify(appID string, domain string, scopes []string) error {
err := f.GetByAppID(appID)
if err != nil {
return err
}
return f.VerifyApp(domain, scopes)
}
func (f *OAuthApp) VerifyApp(domain string, scopes []string) error {
return VerifyApp(f.NgingOauthApp, domain, scopes)
}
func (f *OAuthApp) GetByAppID(appID string, ignoreStatus ...bool) error {
err := f.NgingOauthApp.Get(nil, db.Cond{`app_id`: appID})
if err != nil {
if err == db.ErrNoMoreRows {
err = f.Context().NewError(stdCode.DataNotFound, `appID无效`)
}
return err
}
if len(ignoreStatus) == 0 || !ignoreStatus[0] {
if f.NgingOauthApp.Disabled != common.BoolN {
return f.Context().NewError(stdCode.DataUnavailable, `应用已停用`)
}
}
return err
}
func MatchDomain(domain string, f *dbschema.NgingOauthApp) bool {
if f.Id < 1 {
return false
}
if len(f.SiteDomains) == 0 || len(domain) == 0 {
return true
}
outsiteDomains := strings.Split(f.SiteDomains, `,`)
return com.InSlice(domain, outsiteDomains)
}
func InvalidScope(scopes []string, f *dbschema.NgingOauthApp) (invalidScopes []string) {
if f.Id < 1 {
return []string{`invalid-data`}
}
if len(f.Scopes) == 0 || len(scopes) == 0 {
return
}
allowScopes := strings.Split(f.Scopes, `,`)
for _, scope := range scopes {
if !com.InSlice(scope, allowScopes) {
invalidScopes = append(invalidScopes, scope)
}
}
return
}
func VerifyApp(f *dbschema.NgingOauthApp, domain string, scopes []string) error {
ctx := f.Context()
if !MatchDomain(domain, f) {
return ctx.NewError(stdCode.InvalidType, ctx.T(`应用与域名不匹配`))
}
invalidScopes := InvalidScope(scopes, f)
if len(invalidScopes) > 0 {
return ctx.NewError(stdCode.InvalidType, ctx.T(`应用未获取授权: %s`, strings.Join(invalidScopes, `, `)))
}
return nil
}