Skip to content

Commit a47b240

Browse files
author
Matrix-X
committed
init oauth manager
1 parent 7b01dc4 commit a47b240

File tree

11 files changed

+119
-31
lines changed

11 files changed

+119
-31
lines changed

go.mod

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,11 @@ go 1.16
44

55
replace github.com/ArtisanCloud/go-libs => ../go-libs
66

7+
replace github.com/ArtisanCloud/go-socialite => ../go-socialite
8+
79
require (
8-
github.com/ArtisanCloud/go-libs v1.0.9
10+
github.com/ArtisanCloud/go-libs v1.0.10
11+
github.com/ArtisanCloud/go-socialite v0.0.0-00010101000000-000000000000 // indirect
912
github.com/gin-gonic/gin v1.7.1 // indirect
1013
github.com/go-redis/redis/v8 v8.10.0 // indirect
1114
github.com/mattn/go-isatty v0.0.13 // indirect

src/kernel/baseClient.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ func (client *BaseClient) Request(url string, method string, options *object.Has
8080
if returnRaw {
8181
return response
8282
} else {
83-
config := *(*client.App).GetContainer().GetConfig()
83+
config := *(*client.App).GetContainer().Config
8484
client.CastResponseToType(response, config["response_type"])
8585
}
8686
return response
@@ -114,7 +114,7 @@ func (d *MiddlewareAccessToken) ModifyRequest(req *http2.Request) error {
114114
accessToken := (*d.App).GetAccessToken()
115115

116116
if accessToken != nil {
117-
config := (*d.App).GetContainer().GetConfig()
117+
config := (*d.App).GetContainer().Config
118118
accessToken.ApplyToRequest(req, config)
119119
}
120120

src/kernel/exception/exception.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package exception
2+
3+
import "github.com/ArtisanCloud/go-libs/exception"
4+
5+
type Exception struct {
6+
exception.Exception
7+
}
8+

src/kernel/provider.go

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
package kernel
22

33
func RegisterProvider(app ApplicationInterface) {
4-
container := app.GetContainer()
5-
6-
// first merge configs
7-
container.GetConfig()
8-
9-
// register global http client
10-
11-
// register log service
4+
// tbd
5+
6+
//container := app.GetContainer()
7+
//
8+
//// first merge configs
9+
//container.GetConfig()
10+
//
11+
//// register global http client
12+
//
13+
//// register log service
1214

1315
}

src/kernel/serviceContainer.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"github.com/ArtisanCloud/go-libs/object"
55
)
66

7-
87
type ApplicationInterface interface {
98
GetContainer() *ServiceContainer
109
GetAccessToken() *AccessToken
@@ -18,8 +17,8 @@ type ServiceContainer struct {
1817
Config *object.HashMap
1918
}
2019

21-
func (container *ServiceContainer) getBaseConfig() object.HashMap {
22-
return object.HashMap{
20+
func (container *ServiceContainer) getBaseConfig() *object.HashMap {
21+
return &object.HashMap{
2322
// http://docs.guzzlephp.org/en/stable/request-options.html
2423
"http": object.HashMap{
2524
"timeout": 30.0,
@@ -29,15 +28,17 @@ func (container *ServiceContainer) getBaseConfig() object.HashMap {
2928
}
3029

3130
func (container *ServiceContainer) GetConfig() *object.HashMap {
32-
// if container config has been setup
31+
32+
// init container config
33+
var config *object.HashMap
3334
if container.Config != nil {
34-
return container.Config
35+
config = container.Config
36+
} else {
37+
config = container.getBaseConfig()
3538
}
3639

37-
container.Config = &object.HashMap{}
38-
3940
// merge config
40-
container.Config = object.MergeHashMap(container.Config, container.DefaultConfig, container.UserConfig)
41+
container.Config = object.MergeHashMap(config, container.DefaultConfig, container.UserConfig)
4142

4243
return container.Config
4344
}

src/work/application.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,20 @@ import (
55
"github.com/ArtisanCloud/go-wechat/src/kernel"
66
"github.com/ArtisanCloud/go-wechat/src/work/auth"
77
"github.com/ArtisanCloud/go-wechat/src/work/base"
8+
"github.com/ArtisanCloud/go-wechat/src/work/oauth"
89
)
910

1011
type Work struct {
1112
*kernel.ServiceContainer
1213

1314
Base *base.Client
1415
AccessToken *auth.AccessToken
16+
OAuth *oauth.Manager
1517
}
1618

1719
func NewWork(config *object.HashMap) *Work {
1820
// init an app container
19-
container:=&kernel.ServiceContainer{
21+
container := &kernel.ServiceContainer{
2022
UserConfig: config,
2123
DefaultConfig: &object.HashMap{
2224
"http": map[string]string{
@@ -30,14 +32,13 @@ func NewWork(config *object.HashMap) *Work {
3032
ServiceContainer: container,
3133
}
3234

33-
34-
3535
// register Auth
3636
app.AccessToken = auth.RegisterProvider(app)
3737
// register Base
3838
app.Base = base.RegisterProvider(app)
3939

40-
40+
// register oauth
41+
app.OAuth = oauth.RegisterProvider(app)
4142

4243
return app
4344
}
@@ -46,6 +47,6 @@ func (app *Work) GetContainer() *kernel.ServiceContainer {
4647
return app.ServiceContainer
4748
}
4849

49-
func (app *Work) GetAccessToken() *kernel.AccessToken{
50+
func (app *Work) GetAccessToken() *kernel.AccessToken {
5051
return app.AccessToken.AccessToken
51-
}
52+
}

src/work/base/client.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,26 @@ package base
22

33
import (
44
"github.com/ArtisanCloud/go-wechat/src/kernel"
5+
"github.com/ArtisanCloud/go-wechat/src/work/base/response"
56
)
67

78
type Client struct {
89
*kernel.BaseClient
910
}
1011

11-
func (comp *Client) GetCallbackIp() *ResponseGetCallBackIp {
12+
func (comp *Client) GetCallbackIp() *response.ResponseGetCallBackIp {
1213

13-
result := &ResponseGetCallBackIp{}
14+
result := &response.ResponseGetCallBackIp{}
1415

1516
comp.HttpGet("cgi-bin/getcallbackip", nil, result)
1617

1718
return result
1819
}
1920

2021

21-
func (comp *Client) GetAPIDomainIP() *ResponseGetAPIDomainIP {
22+
func (comp *Client) GetAPIDomainIP() *response.ResponseGetAPIDomainIP {
2223

23-
result := &ResponseGetAPIDomainIP{}
24+
result := &response.ResponseGetAPIDomainIP{}
2425

2526
comp.HttpGet("cgi-bin/get_api_domain_ip", nil, result)
2627

src/work/base/responseGetAPIDomainIP.go renamed to src/work/base/response/responseGetAPIDomainIP.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package base
1+
package response
22

33
import (
44
"github.com/ArtisanCloud/go-wechat/src/kernel/response"

src/work/base/responseGetCallbackIp.go renamed to src/work/base/response/responseGetCallbackIp.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package base
1+
package response
22

33
import (
44
"github.com/ArtisanCloud/go-wechat/src/kernel/response"

src/work/oauth/manager.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,24 @@
11
package oauth
22

3+
import (
4+
"github.com/ArtisanCloud/go-libs/object"
5+
"github.com/ArtisanCloud/go-socialite/src/providers"
6+
"github.com/ArtisanCloud/go-wechat/src/kernel"
7+
)
38

9+
type Manager struct {
10+
Config *object.HashMap
11+
App *kernel.ApplicationInterface
12+
Provider *providers.WeCom
13+
}
14+
15+
func NewManager(config *object.HashMap,app *kernel.ApplicationInterface) *Manager {
16+
17+
manager:= &Manager{
18+
config,
19+
app,
20+
nil,
21+
}
22+
return manager
23+
24+
}

src/work/oauth/provider.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,54 @@
11
package oauth
22

3+
import (
4+
"fmt"
5+
"github.com/ArtisanCloud/go-libs/object"
6+
"github.com/ArtisanCloud/go-wechat/src/kernel"
7+
"github.com/ArtisanCloud/go-wechat/src/kernel/exception"
8+
"strings"
9+
)
310

11+
func RegisterProvider(app kernel.ApplicationInterface) *Manager {
12+
13+
config := *app.GetContainer().Config
14+
15+
manager := NewManager(&object.HashMap{
16+
"wecom": &object.HashMap{
17+
"client_id": config["corp_id"].(string),
18+
"client_secret": "",
19+
"corp_id": config["corp_id"].(string),
20+
"corp_secret": config["secret"].(string),
21+
"redirect": prepareCallbackUrl(app),
22+
},
23+
}, &app)
24+
25+
scopes:= []string{
26+
"snsapi_base",
27+
}
28+
29+
if config["oauth.scopes"] !=nil{
30+
scopes = config["oauth.scopes"].([]string)
31+
}
32+
33+
if len(scopes)>0{
34+
35+
}
36+
37+
return manager
38+
39+
}
40+
41+
func prepareCallbackUrl(app kernel.ApplicationInterface) string {
42+
config := *app.GetContainer().Config
43+
44+
callback := config["oauth.callback"].(string)
45+
if strings.Index(callback, "http") == 0 {
46+
return callback
47+
} else {
48+
// have to setup a complete url with host
49+
defer (&exception.Exception{}).HandleException(nil, "oauth.prepare.callback.url", nil)
50+
panic(fmt.Sprintf("OAuth callback format invalid, please make sure that schema 'http' added: %v", callback))
51+
52+
}
53+
54+
}

0 commit comments

Comments
 (0)