/
admin.go
199 lines (181 loc) · 5.72 KB
/
admin.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
package admin
import (
"encoding/base64"
"encoding/json"
"fmt"
"github.com/COMTOP1/api/controllers"
"github.com/COMTOP1/api/services/admin"
"github.com/COMTOP1/api/utils"
"github.com/couchbase/gocb/v2"
"github.com/golang-jwt/jwt"
"github.com/labstack/echo/v4"
uuid "github.com/satori/go.uuid"
"net/http"
"strings"
"time"
)
type (
Repo struct {
admin *admin.Store
controller controllers.Controller
domainName string
url string
signingToken []byte
}
Claim struct {
jwt.StandardClaims
}
Token struct {
jwt.Token
}
// Token struct {
// Raw string // The raw token. Populated when you Parse a token
// Method SigningMethod // The signing method used or to be used
// Header map[string]interface{} // The first segment of the token
// Claims Claims // The second segment of the token
// Signature string // The third segment of the token. Populated when you Parse a token
// Valid bool // Is the token valid? Populated when you Parse/Verify a token
// }
SigningMethod interface {
Verify(signingString, signature string, key interface{}) error // Returns nil if signature is valid
Sign(signingString string, key interface{}) (string, error) // Returns encoded signature or error
Alg() string // returns the alg identifier for this method (example: 'HS256')
}
Claims interface {
Valid() error
}
JWTToken struct {
JWTToken string `json:"jwt_token"`
}
)
func NewRepo(scope *gocb.Scope, controller controllers.Controller, domainName string, URL string, signingToken []byte) *Repo {
return &Repo{
admin: admin.NewStore(scope),
controller: controller,
domainName: domainName,
url: URL,
signingToken: signingToken,
}
}
func (r *Repo) GetAdminURL() string {
return r.url
}
func (r *Repo) GetJWT(c echo.Context) error {
timeNow := time.Now()
expirationTime := timeNow.Add(2 * time.Minute).Unix()
uuid1 := uuid.NewV4()
claim := &jwt.StandardClaims{
Audience: "https://api." + r.domainName,
ExpiresAt: expirationTime,
Id: uuid1.String(),
IssuedAt: timeNow.Unix(),
Issuer: "https://api." + r.domainName,
NotBefore: timeNow.Unix(),
}
token := NewWithClaims(jwt.SigningMethodHS512, claim)
tokenString, err := token.SignedString(r.signingToken)
if err != nil {
err = fmt.Errorf("GetJWT failed: %w", err)
return echo.NewHTTPError(http.StatusInternalServerError, utils.Error{Error: err.Error()})
}
b, err := json.Marshal(JWTToken{JWTToken: tokenString})
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, utils.Error{Error: err.Error()})
}
_, err = c.Response().Write(b)
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, utils.Error{Error: err.Error()})
}
return c.NoContent(http.StatusAccepted)
}
func (r *Repo) GetSSOJWT(c echo.Context) error {
timeNow := time.Now()
expirationTime := timeNow.Add(72 * time.Hour).Unix()
uuid1 := uuid.NewV4().String()
claim := &jwt.StandardClaims{
Audience: "https://sso." + r.domainName,
Id: uuid1,
IssuedAt: timeNow.Unix(),
Issuer: "https://api." + r.domainName,
ExpiresAt: expirationTime,
NotBefore: timeNow.Unix(),
}
token := NewWithClaims(jwt.SigningMethodHS512, claim)
tokenString, err := token.SignedString(r.signingToken)
if err != nil {
err = fmt.Errorf("GetJWT failed: %w", err)
return echo.NewHTTPError(http.StatusInternalServerError, utils.Error{Error: err.Error()})
}
kid := fmt.Sprintf("%v", token.Header["kid"])
err = r.admin.AddSSOJWT(claim, kid, c.Request().UserAgent())
if err != nil {
err = fmt.Errorf("getSSOJWT error: %w", err)
return echo.NewHTTPError(http.StatusInternalServerError, utils.Error{Error: err.Error()})
}
b, err := json.Marshal(JWTToken{JWTToken: tokenString})
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, utils.Error{Error: err.Error()})
}
_, err = c.Response().Write(b)
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, utils.Error{Error: err.Error()})
}
return c.NoContent(http.StatusAccepted)
}
func (r *Repo) VerifySSO(c echo.Context) error {
kid, claims, err := r.controller.Access.GetAdminTokenKIDAndClaims(c.Request())
if err != nil {
err = fmt.Errorf("verifySSOJWT error: %w", err)
return echo.NewHTTPError(http.StatusInternalServerError, utils.Error{Error: err.Error()})
}
err = r.admin.VerifySSOJWT(claims, kid, c.Request().UserAgent())
if err != nil {
err = fmt.Errorf("verifySSOJWT error: %w", err)
return echo.NewHTTPError(http.StatusInternalServerError, utils.Error{Error: err.Error()})
}
return c.NoContent(http.StatusAccepted)
}
func NewWithClaims(method SigningMethod, claims Claims) *jwt.Token {
uuid1 := uuid.NewV4()
return &jwt.Token{
Header: map[string]interface{}{
"typ": "JWT",
"kid": uuid1.String(),
"alg": method.Alg(),
},
Claims: claims,
Method: method,
}
}
func (t *Token) SignedString(key interface{}) (string, error) {
var sig, sstr string
var err error
if sstr, err = t.SigningString(); err != nil {
return "", err
}
if sig, err = t.Method.Sign(sstr, key); err != nil {
return "", err
}
return strings.Join([]string{sstr, sig}, "."), nil
}
func (t *Token) SigningString() (string, error) {
var err error
parts := make([]string, 2)
for i := range parts {
var jsonValue []byte
if i == 0 {
if jsonValue, err = json.Marshal(t.Header); err != nil {
return "", err
}
} else {
if jsonValue, err = json.Marshal(t.Claims); err != nil {
return "", err
}
}
parts[i] = EncodeSegment(jsonValue)
}
return strings.Join(parts, "."), nil
}
func EncodeSegment(seg []byte) string {
return base64.RawURLEncoding.EncodeToString(seg)
}