-
Notifications
You must be signed in to change notification settings - Fork 3
/
mocks_token.go
121 lines (104 loc) · 2.98 KB
/
mocks_token.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
// Copyright 2023 Cisco Systems, Inc. and its affiliates
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// SPDX-License-Identifier: Apache-2.0
package sectest
import (
"encoding/base64"
"encoding/json"
"github.com/cisco-open/go-lanai/pkg/security/oauth2"
"github.com/cisco-open/go-lanai/pkg/utils"
"strings"
"time"
)
const (
tokenDelimiter = "~"
)
/*************************
Token
*************************/
type MockedTokenInfo struct {
ClientID string `json:"ClientID"`
UName string `json:"UName"`
UID string `json:"UID"`
TID string `json:"TID"`
TExternalId string `json:"TExternalId"`
OrigU string `json:"OrigU"`
Exp int64 `json:"Exp"`
Iss int64 `json:"Iss"`
Scopes []string `json:"Scopes"`
}
// MockedToken implements oauth2.AccessToken
type MockedToken struct {
MockedTokenInfo
Token string
ExpTime time.Time `json:"-"`
IssTime time.Time `json:"-"`
}
func (mt MockedToken) MarshalText() (text []byte, err error) {
if len(mt.Token) != 0 {
return []byte(mt.Token), nil
}
mt.Exp = mt.ExpTime.UnixNano()
mt.Iss = mt.IssTime.UnixNano()
text, err = json.Marshal(mt.MockedTokenInfo)
if err != nil {
return
}
return []byte(base64.StdEncoding.EncodeToString(text)), nil
}
func (mt *MockedToken) UnmarshalText(text []byte) error {
data, e := base64.StdEncoding.DecodeString(string(text))
if e != nil {
return e
}
if e := json.Unmarshal(data, &mt.MockedTokenInfo); e != nil {
return e
}
mt.ExpTime = time.Unix(0, mt.Exp)
mt.IssTime = time.Unix(0, mt.Iss)
return nil
}
func (mt MockedToken) String() string {
vals := []string{mt.UName, mt.UID, mt.TID, mt.TExternalId, mt.OrigU, mt.ExpTime.Format(utils.ISO8601Milliseconds)}
return strings.Join(vals, tokenDelimiter)
}
func (mt *MockedToken) Value() string {
text, e := mt.MarshalText()
if e != nil {
return ""
}
return string(text)
}
func (mt *MockedToken) ExpiryTime() time.Time {
return mt.ExpTime
}
func (mt *MockedToken) Expired() bool {
return !mt.ExpTime.IsZero() && !time.Now().Before(mt.ExpTime)
}
func (mt *MockedToken) Details() map[string]interface{} {
return map[string]interface{}{}
}
func (mt *MockedToken) Type() oauth2.TokenType {
return oauth2.TokenTypeBearer
}
func (mt *MockedToken) IssueTime() time.Time {
return mt.IssTime
}
func (mt *MockedToken) Scopes() utils.StringSet {
return utils.NewStringSet(mt.MockedTokenInfo.Scopes...)
}
func (mt *MockedToken) RefreshToken() oauth2.RefreshToken {
return nil
}