-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
136 lines (126 loc) · 3.37 KB
/
server.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
package oauth
import (
"crypto"
"crypto/aes"
"crypto/cipher"
"crypto/rsa"
"github.com/bootapp/oauth2/errors"
"github.com/bootapp/oauth2/generates"
"github.com/bootapp/oauth2/manage"
"github.com/bootapp/oauth2/models"
"github.com/bootapp/oauth2/server"
"github.com/bootapp/oauth2/store"
core "srv-core/proto"
"srv-core/settings"
"github.com/dgrijalva/jwt-go"
"github.com/golang/glog"
_ "golang.org/x/crypto/sha3"
"google.golang.org/grpc"
"log"
"time"
)
type UserPassOAuthServer struct {
privKey *rsa.PrivateKey
pubKey *rsa.PublicKey
Srv *server.Server
clientStore *store.ClientStore
manager *manage.StatelessManager
Hash crypto.Hash
aesKey []byte
aesCipher cipher.Block
}
var (
dalCoreUserClient core.DalUserServiceClient
dalCoreUserConn *grpc.ClientConn
oauthServer *UserPassOAuthServer
)
func NewPassOAuthServer() *UserPassOAuthServer {
if oauthServer != nil {
return oauthServer
}
oauthServer = &UserPassOAuthServer{}
oauthServer.Init()
return oauthServer
}
func (s *UserPassOAuthServer) Init() {
s.manager = manage.NewStatelessManager()
s.clientStore = store.NewClientStore()
s.manager.MapClientStorage(s.clientStore)
// Authorize Code Expire Time
s.manager.SetAuthorizeCodeExp(time.Minute * 10)
// Password Type Settings
cfg := &manage.Config {
// access token expiration time
AccessTokenExp: time.Hour * 2,
// refresh token expiration time
RefreshTokenExp: time.Hour * 24 * 7,
// whether to generate the refreshing token
IsGenerateRefresh: true,
}
cfgRefresh := &manage.RefreshingConfig{
RefreshTokenExp: time.Hour * 24 * 7,
IsGenerateRefresh: true,
AccessTokenExp: time.Hour * 2,
}
s.manager.SetRefreshTokenCfg(cfgRefresh)
s.manager.SetPasswordTokenCfg(cfg)
s.Srv = server.NewDefaultServer(s.manager)
s.Srv.SetAllowGetAccessRequest(true)
s.Srv.SetClientInfoHandler(server.ClientFormHandler)
s.Srv.SupportedScope = "user_rw"
s.Srv.SetInternalErrorHandler(func(err error) (re *errors.Response) {
glog.Error("Internal Error:", err.Error())
return
})
s.Srv.SetResponseErrorHandler(func(re *errors.Response) {
glog.Error("Response Error:", re.Error.Error())
})
s.Srv.SetPasswordAuthorizationHandler(loginHandler)
s.Hash = crypto.SHA3_256
s.aesKey = []byte(settings.SignerAESKey)
var err error
s.aesCipher, err = aes.NewCipher(s.aesKey)
if err != nil {
panic(err)
}
}
func GetOauthServer() *UserPassOAuthServer {
return oauthServer
}
func (s * UserPassOAuthServer) SetupUserClient(dalCoreUserAddr string) {
var err error
dalCoreUserConn, err = grpc.Dial(dalCoreUserAddr, grpc.WithInsecure())
if err != nil {
log.Fatalf("did not connect: %v", err)
}
dalCoreUserClient = core.NewDalUserServiceClient(dalCoreUserConn)
}
func (s * UserPassOAuthServer) UpdateClientStore(clients map[string]string) {
var err error
for key, value := range clients{
err = s.clientStore.Set(key, &models.Client {
ID: key,
Secret: value,
Domain: "http://localhost",
})
if err != nil {
panic(err)
}
}
}
func (s *UserPassOAuthServer) SetRSAKeyFromPem(pem []byte) {
var err error
s.privKey, err = jwt.ParseRSAPrivateKeyFromPEM(pem)
if err != nil {
panic(err)
}
s.pubKey = &s.privKey.PublicKey
s.manager.MapAccessGenerate(generates.NewJWTAccessGenerate(pem, jwt.SigningMethodRS256))
}
func (s *UserPassOAuthServer) GetPublicKey() []byte {
pubKey, err:= EncodePublicKey(s.pubKey)
if err != nil {
glog.Fatal(err)
}
return pubKey
}