-
-
Notifications
You must be signed in to change notification settings - Fork 199
/
client.go
74 lines (66 loc) · 1.86 KB
/
client.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
package oauth2server
import (
"context"
"net/url"
"strings"
"github.com/admpub/nging/v5/application/model"
"github.com/admpub/oauth2/v4"
"github.com/admpub/oauth2/v4/models"
"github.com/coscms/oauth2s"
"github.com/webx-top/com"
"github.com/webx-top/echo"
"github.com/webx-top/echo/code"
"github.com/webx-top/echo/defaults"
)
var (
DefaultAppClient oauth2.ClientStore = NewClientStore(nil)
Default = oauth2s.NewConfig()
)
var requestFormDataCacheKey = `backend.` + oauth2s.RequestFormDataCacheKey
func NewClientStore(fn func(ctx echo.Context, appID string) (oauth2.ClientInfo, error)) oauth2.ClientStore {
if fn == nil {
fn = getByAppID
}
return &AppClient{GetByAppID: fn}
}
func getByAppID(ctx echo.Context, appID string) (oauth2.ClientInfo, error) {
openM := model.NewOAuthApp(ctx)
err := openM.GetByAppID(appID)
if err != nil {
return nil, err
}
scope := ctx.Form("scope")
var scopes []string
if len(scope) > 0 {
scopes = strings.Split(scope, " ")
}
redirectURI := ctx.Form("redirect_uri")
u, err := url.Parse(redirectURI)
if err != nil {
return nil, ctx.NewError(code.InvalidParameter, `参数 %s 不是有效的网址`, `redirect_uri`)
}
domain := com.SplitHost(u.Host)
err = openM.VerifyApp(domain, scopes)
if err != nil {
return nil, err
}
clientInfo := &models.Client{
ID: openM.AppId,
Secret: openM.AppSecret,
UserID: ``,
}
if len(openM.SiteDomains) > 0 {
clientInfo.Domain = strings.SplitN(openM.SiteDomains, `,`, 2)[0]
}
return clientInfo, nil
}
type AppClient struct {
GetByAppID func(ctx echo.Context, appID string) (oauth2.ClientInfo, error)
}
func (a *AppClient) GetByID(stdCtx context.Context, appID string) (oauth2.ClientInfo, error) {
ctx := defaults.MustGetContext(stdCtx)
if Debug {
println(`[AppClient.GetByID.Forms]:`, echo.Dump(ctx.Forms(), false))
}
return a.GetByAppID(ctx, appID)
}