-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathoauth.go
47 lines (39 loc) · 931 Bytes
/
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
package simpleoauth
import (
"fmt"
)
var oauthes= make(map[string]OAuth)
type OAuth interface{
GetAccesstoken(code string) map[string]interface{}
GetUserinfo(accesstoken string, openid string) map[string]interface{}
Authorize(code string) AuthorizeResult
InitOAuth()
}
func ReisterPlatform(name string, oauth OAuth){
if oauth == nil {
panic("Register simpleoauth instance is nil")
}
_, dup := oauthes[name]
if dup{
panic("The platform has registered already")
}
oauthes[name] = oauth
}
type Manager struct {
oauth OAuth
}
func NewManager(platformName string)(*Manager, error){
oauth, ok := oauthes[platformName]
if !ok{
return nil, fmt.Errorf("unknown platform %q", platformName)
}
oauth.InitOAuth()
return &Manager{oauth}, nil
}
func (m *Manager)Authorize(code string) AuthorizeResult{
return m.oauth.Authorize(code)
}
type AuthorizeResult struct{
Result bool
Userinfo map[string]interface{}
}