summaryrefslogtreecommitdiff
path: root/internal/suites/suites_credentials.go
blob: 50e83805b181b3a43966832f1fc88f069cea49b8 (plain)
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
package suites

import (
	"sync"
	"time"

	"github.com/authelia/otp/totp"
	"github.com/go-rod/rod/lib/proto"
)

func NewRodSuiteCredentials() *RodSuiteCredentials {
	return &RodSuiteCredentials{
		lock: &sync.Mutex{},
		totp: map[string]RodSuiteCredentialOneTimePassword{},
	}
}

type RodSuiteCredentials struct {
	lock     *sync.Mutex
	totp     map[string]RodSuiteCredentialOneTimePassword
	webauthn RodSuiteCredentialWebAuthn
}

type RodSuiteCredentialWebAuthn struct {
	AuthenticatorID proto.WebAuthnAuthenticatorID
	Credentials     []*proto.WebAuthnCredential
}

func (rsc *RodSuiteCredentials) GetOneTimePassword(username string) RodSuiteCredentialOneTimePassword {
	rsc.lock.Lock()

	defer rsc.lock.Unlock()

	return rsc.totp[username]
}

func (rsc *RodSuiteCredentials) SetOneTimePassword(username string, credential RodSuiteCredentialOneTimePassword) {
	rsc.lock.Lock()

	defer rsc.lock.Unlock()

	credential.valid = true

	rsc.totp[username] = credential
}

func (rsc *RodSuiteCredentials) DeleteOneTimePassword(username string) {
	rsc.lock.Lock()

	defer rsc.lock.Unlock()

	rsc.totp[username] = RodSuiteCredentialOneTimePassword{
		valid: false,
	}
}

func (rsc *RodSuiteCredentials) GetWebAuthnAuthenticatorID() (authenticatorID proto.WebAuthnAuthenticatorID) {
	rsc.lock.Lock()

	defer rsc.lock.Unlock()

	return rsc.webauthn.AuthenticatorID
}

func (rsc *RodSuiteCredentials) GetWebAuthnCredentials() (credentials []*proto.WebAuthnCredential) {
	rsc.lock.Lock()

	defer rsc.lock.Unlock()

	return rsc.webauthn.Credentials
}

func (rsc *RodSuiteCredentials) GetWebAuthnAuthenticator() (authenticatorID proto.WebAuthnAuthenticatorID, credentials []*proto.WebAuthnCredential) {
	rsc.lock.Lock()

	defer rsc.lock.Unlock()

	return rsc.webauthn.AuthenticatorID, rsc.webauthn.Credentials
}

func (rsc *RodSuiteCredentials) SetWebAuthnAuthenticatorID(authenticatorID proto.WebAuthnAuthenticatorID) {
	rsc.lock.Lock()

	defer rsc.lock.Unlock()

	rsc.webauthn.AuthenticatorID = authenticatorID
}

func (rsc *RodSuiteCredentials) SetWebAuthnAuthenticatorCredentials(credentials ...*proto.WebAuthnCredential) {
	rsc.lock.Lock()

	defer rsc.lock.Unlock()

	rsc.webauthn.Credentials = nil

	for _, credential := range credentials {
		if credential.RpID == "" {
			continue
		}

		rsc.webauthn.Credentials = append(rsc.webauthn.Credentials, credential)
	}
}

func (rsc *RodSuiteCredentials) DeleteWebAuthnAuthenticatorCredentials() {
	rsc.lock.Lock()

	defer rsc.lock.Unlock()

	rsc.webauthn.Credentials = nil
}

func (rsc *RodSuiteCredentials) UpdateWebAuthnAuthenticator(funcUpdate func(authenticatorID proto.WebAuthnAuthenticatorID, credentials []*proto.WebAuthnCredential) (proto.WebAuthnAuthenticatorID, []*proto.WebAuthnCredential)) {
	if funcUpdate == nil {
		return
	}

	rsc.lock.Lock()

	defer rsc.lock.Unlock()

	credentials := make([]*proto.WebAuthnCredential, len(rsc.webauthn.Credentials))

	if len(credentials) != 0 {
		copy(credentials, rsc.webauthn.Credentials)
	}

	rsc.webauthn.AuthenticatorID, rsc.webauthn.Credentials = funcUpdate(rsc.webauthn.AuthenticatorID, credentials)
}

func (rsc *RodSuiteCredentials) DeleteWebAuthnAuthenticator() {
	rsc.lock.Lock()

	defer rsc.lock.Unlock()

	rsc.webauthn = RodSuiteCredentialWebAuthn{}
}

type RodSuiteCredentialsProvider interface {
	GetOneTimePassword(username string) RodSuiteCredentialOneTimePassword
	SetOneTimePassword(username string, credential RodSuiteCredentialOneTimePassword)
	DeleteOneTimePassword(username string)

	GetWebAuthnAuthenticatorID() (authenticatorID proto.WebAuthnAuthenticatorID)
	GetWebAuthnCredentials() (credentials []*proto.WebAuthnCredential)
	SetWebAuthnAuthenticatorID(authenticatorID proto.WebAuthnAuthenticatorID)
	SetWebAuthnAuthenticatorCredentials(credentials ...*proto.WebAuthnCredential)
	DeleteWebAuthnAuthenticatorCredentials()
	GetWebAuthnAuthenticator() (authenticatorID proto.WebAuthnAuthenticatorID, credentials []*proto.WebAuthnCredential)
	UpdateWebAuthnAuthenticator(funcUpdate func(authenticatorID proto.WebAuthnAuthenticatorID, credentials []*proto.WebAuthnCredential) (proto.WebAuthnAuthenticatorID, []*proto.WebAuthnCredential))
	DeleteWebAuthnAuthenticator()
}

type RodSuiteCredentialOneTimePassword struct {
	valid             bool
	Secret            string
	ValidationOptions totp.ValidateOpts
}

func (otp *RodSuiteCredentialOneTimePassword) Valid() bool {
	return otp.valid
}

func (otp *RodSuiteCredentialOneTimePassword) Generate(at time.Time) (passcode string, err error) {
	return totp.GenerateCodeCustom(otp.Secret, at, otp.ValidationOptions)
}