-
Notifications
You must be signed in to change notification settings - Fork 0
/
customize.go
125 lines (117 loc) · 3.99 KB
/
customize.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
package oauth
import (
"context"
"encoding/json"
"github.com/bootapp/rest-grpc-oauth2/auth"
"github.com/dgrijalva/jwt-go"
"github.com/golang/glog"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"net/http"
"regexp"
core "srv-core/proto"
"strconv"
"strings"
)
var orgNameRegex *regexp.Regexp
func procQueryUserResp(resp *core.UserWithOrgAuth) (userID int64, orgID int64, authorities map[int64][]int64, err error) {
if orgNameRegex == nil {
orgNameRegex, err = regexp.Compile("[|:]")
}
if err != nil {
return 0, 0, nil, err
}
if resp.User == nil || len(resp.OrgInfo) == 0 {
return 0, 0, nil, status.Error(codes.Internal, "INTERNAL:error implementation of queryUser")
}
if len(resp.OrgInfo) > 1 {
result := ""
for idx, orgInfo := range resp.OrgInfo {
if idx == 0 {
result += strconv.FormatInt(orgInfo.Id, 10) + ":"+ orgNameRegex.ReplaceAllString(orgInfo.Name, "")
} else {
result += "|" + strconv.FormatInt(orgInfo.Id, 10) + ":"+ orgNameRegex.ReplaceAllString(orgInfo.Name, "")
}
}
return 0, 0, nil, status.Error(codes.FailedPrecondition, result)
} else {
authorities, err = auth.AuthorityEncode(strings.Split(resp.OrgInfo[0].AuthorityGroups, ";"), strings.Split(resp.OrgInfo[0].Authorities, ";"))
return resp.User.Id, resp.OrgInfo[0].Id, authorities, err
}
}
func loginHandler(username, password, code, orgId, authType string) (userID int64, orgID int64, authorities map[int64][]int64, err error) {
glog.Info("authenticating user...")
orgIdNum, err := strconv.ParseInt(orgId, 10, 64)
if err != nil {
orgIdNum = 0
}
switch authType {
case "LOGIN_TYPE_USERNAME_PASS":
resp, err := dalCoreUserClient.ReadUserAuth(context.Background(), &core.ReadUserReq{Username:username,
Password: password, OrgId:orgIdNum})
if err != nil {
return 0, 0, nil, err
}
return procQueryUserResp(resp)
case "LOGIN_TYPE_EMAIL_PASS":
resp, err := dalCoreUserClient.ReadUserAuth(context.Background(), &core.ReadUserReq{Email:username,
Password: password, OrgId:orgIdNum})
if err != nil {
return 0, 0, nil, err
}
return procQueryUserResp(resp)
case "LOGIN_TYPE_PHONE_PASS":
resp, err := dalCoreUserClient.ReadUserAuth(context.Background(), &core.ReadUserReq{Phone:username,
Password:password, OrgId:orgIdNum})
if err != nil {
return 0, 0, nil, err
}
return procQueryUserResp(resp)
case "LOGIN_TYPE_ANY_PASS":
resp, err := dalCoreUserClient.ReadUserAuth(context.Background(),
&core.ReadUserReq{Phone:username, Email:username,
Username:username, Password:password, OrgId:orgIdNum})
if err != nil {
return 0, 0, nil, err
}
return procQueryUserResp(resp)
case "LOGIN_TYPE_PHONE_CODE":
return 0, 0, nil, status.Error(codes.Unimplemented, "INTERNAL:not implemented yet")
case "LOGIN_TYPE_PHONE_LOGIN_OR_REG":
return 0, 0, nil, status.Error(codes.Unimplemented, "INTERNAL:not implemented yet")
}
return 0, 0, nil, status.Error(codes.Unimplemented, "INTERNAL:not implemented yet")
}
func ServeOAuthHTTP(w http.ResponseWriter, r *http.Request) {
switch r.URL.Path {
case "/api/oauth/authorize":
err := oauthServer.Srv.HandleAuthorizeRequest(w, r)
if err != nil {
stat := status.Convert(err)
http.Error(w, stat.Message(), http.StatusBadRequest)
}
case "/api/oauth/token":
err := oauthServer.Srv.HandleTokenRequest(w, r)
if err != nil {
stat := status.Convert(err)
if stat.Code() == codes.FailedPrecondition {
http.Error(w, stat.Message(), http.StatusMultipleChoices)
} else {
http.Error(w, stat.Message(), http.StatusBadRequest)
}
}
case "/api/oauth/token_key":
w.Header().Set("Content-Type", "application/json;charset=UTF-8")
w.Header().Set("Cache-Control", "no-store")
w.Header().Set("Pragma", "no-cache")
w.WriteHeader(http.StatusOK)
resp := make(map[string] string)
resp["alg"] = jwt.SigningMethodRS256.Name
resp["value"] = string(oauthServer.GetPublicKey())
err := json.NewEncoder(w).Encode(resp)
if err != nil {
stat := status.Convert(err)
http.Error(w, stat.Message(), http.StatusBadRequest)
}
}
}