-
Notifications
You must be signed in to change notification settings - Fork 129
/
authenticator_model.go
181 lines (146 loc) · 4.62 KB
/
authenticator_model.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
/*
Copyright NetFoundry Inc.
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
https://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.
*/
package model
import (
"encoding/base64"
"github.com/michaelquigley/pfxlog"
"github.com/openziti/storage/boltz"
"github.com/openziti/ziti/controller/db"
"github.com/openziti/ziti/controller/models"
"go.etcd.io/bbolt"
"reflect"
)
type Authenticator struct {
models.BaseEntity
Method string
IdentityId string
SubType interface{}
}
type AuthenticatorSelf struct {
models.BaseEntity
CurrentPassword string
NewPassword string
IdentityId string
Username string
}
func (entity *Authenticator) Fingerprints() []string {
switch entity.SubType.(type) {
case *AuthenticatorCert:
cert, _ := entity.SubType.(*AuthenticatorCert)
return []string{cert.Fingerprint}
default:
return nil
}
}
func (entity *Authenticator) fillFrom(_ Env, _ *bbolt.Tx, boltAuthenticator *db.Authenticator) error {
entity.FillCommon(boltAuthenticator)
entity.Method = boltAuthenticator.Type
entity.IdentityId = boltAuthenticator.IdentityId
boltSubType := boltAuthenticator.ToSubType()
switch boltAuth := boltSubType.(type) {
case *db.AuthenticatorUpdb:
entity.SubType = &AuthenticatorUpdb{
Authenticator: entity,
Username: boltAuth.Username,
Password: boltAuth.Password,
Salt: boltAuth.Salt,
}
case *db.AuthenticatorCert:
entity.SubType = &AuthenticatorCert{
Authenticator: entity,
Fingerprint: boltAuth.Fingerprint,
Pem: boltAuth.Pem,
UnverifiedPem: boltAuth.UnverifiedPem,
UnverifiedFingerprint: boltAuth.UnverifiedFingerprint,
}
default:
pfxlog.Logger().Panicf("unexpected type %v when filling model %s", reflect.TypeOf(boltSubType), "authenticator")
}
return nil
}
func (entity *Authenticator) toBoltEntity() (*db.Authenticator, error) {
boltEntity := &db.Authenticator{
BaseExtEntity: *boltz.NewExtEntity(entity.Id, entity.Tags),
Type: entity.Method,
IdentityId: entity.IdentityId,
}
var subType db.AuthenticatorSubType
switch entity.SubType.(type) {
case *AuthenticatorUpdb:
updbModel, ok := entity.SubType.(*AuthenticatorUpdb)
if !ok {
pfxlog.Logger().Panicf("unexpected type assertion failure to updb authenticator conversion to bolt model for type %s", reflect.TypeOf(entity.SubType))
}
subType = &db.AuthenticatorUpdb{
Authenticator: *boltEntity,
Username: updbModel.Username,
Password: updbModel.Password,
Salt: updbModel.Salt,
}
case *AuthenticatorCert:
certModel, ok := entity.SubType.(*AuthenticatorCert)
if !ok {
pfxlog.Logger().Panicf("unexpected type assertion failure to cert authenticator conversion to bolt model for type %s", reflect.TypeOf(entity.SubType))
}
subType = &db.AuthenticatorCert{
Authenticator: *boltEntity,
Fingerprint: certModel.Fingerprint,
Pem: certModel.Pem,
UnverifiedFingerprint: certModel.UnverifiedFingerprint,
UnverifiedPem: certModel.UnverifiedPem,
}
default:
pfxlog.Logger().Panicf("unexpected type %v when converting to bolt model authenticator", reflect.TypeOf(entity.SubType))
}
boltEntity.SubType = subType
return boltEntity, nil
}
func (entity *Authenticator) toBoltEntityForCreate(*bbolt.Tx, Env) (*db.Authenticator, error) {
return entity.toBoltEntity()
}
func (entity *Authenticator) toBoltEntityForUpdate(*bbolt.Tx, Env, boltz.FieldChecker) (*db.Authenticator, error) {
return entity.toBoltEntity()
}
func (entity *Authenticator) ToCert() *AuthenticatorCert {
cert, ok := entity.SubType.(*AuthenticatorCert)
if !ok {
return nil
}
cert.Authenticator = entity
return cert
}
func (entity *Authenticator) ToUpdb() *AuthenticatorUpdb {
updb, ok := entity.SubType.(*AuthenticatorUpdb)
if !ok {
return nil
}
updb.Authenticator = entity
return updb
}
type AuthenticatorCert struct {
*Authenticator
Fingerprint string
Pem string
UnverifiedFingerprint string
UnverifiedPem string
}
type AuthenticatorUpdb struct {
*Authenticator
Username string
Password string
Salt string
}
func (au *AuthenticatorUpdb) DecodedSalt() []byte {
result, _ := base64.StdEncoding.DecodeString(au.Salt)
return result
}