Debian Codegraph @

credentials/credentials.go

raw symbols SCIP
1 // Package credentials provides credentials management for Kerberos 5 authentication.
2 package credentials
3
4 import (
5 "time"
6
7 "github.com/hashicorp/go-uuid"
8 "gopkg.in/jcmturner/gokrb5.v5/iana/nametype"
9 "gopkg.in/jcmturner/gokrb5.v5/keytab"
10 "gopkg.in/jcmturner/gokrb5.v5/types"
11 )
12
13 const (
14 // AttributeKeyADCredentials assigned number for AD credentials.
15 AttributeKeyADCredentials = 1
16 )
17
18 // Credentials struct for a user.
19 // Contains either a keytab, password or both.
20 // Keytabs are used over passwords if both are defined.
21 type Credentials struct {
22 Username string
23 displayName string
24 Realm string
25 CName types.PrincipalName
26 Keytab keytab.Keytab
27 Password string
28 Attributes map[int]interface{}
29 ValidUntil time.Time
30
31 authenticated bool
32 human bool
33 authTime time.Time
34 groupMembership map[string]bool
35 sessionID string
36 }
37
38 // ADCredentials contains information obtained from the PAC.
39 type ADCredentials struct {
40 EffectiveName string
41 FullName string
42 UserID int
43 PrimaryGroupID int
44 LogOnTime time.Time
45 LogOffTime time.Time
46 PasswordLastSet time.Time
47 GroupMembershipSIDs []string
48 LogonDomainName string
49 LogonDomainID string
50 LogonServer string
51 }
52
53 // NewCredentials creates a new Credentials instance.
54 func NewCredentials(username string, realm string) Credentials {
55 uid, err := uuid.GenerateUUID()
56 if err != nil {
57 uid = "00unique-sess-ions-uuid-unavailable0"
58 }
59 return Credentials{
60 Username: username,
61 displayName: username,
62 Realm: realm,
63 CName: types.NewPrincipalName(nametype.KRB_NT_PRINCIPAL, username),
64 Keytab: keytab.NewKeytab(),
65 Attributes: make(map[int]interface{}),
66 sessionID: uid,
67 }
68 }
69
70 // NewCredentialsFromPrincipal creates a new Credentials instance with the user details provides as a PrincipalName type.
71 func NewCredentialsFromPrincipal(cname types.PrincipalName, realm string) Credentials {
72 uid, err := uuid.GenerateUUID()
73 if err != nil {
74 uid = "00unique-sess-ions-uuid-unavailable0"
75 }
76 return Credentials{
77 Username: cname.GetPrincipalNameString(),
78 displayName: cname.GetPrincipalNameString(),
79 Realm: realm,
80 CName: cname,
81 Keytab: keytab.NewKeytab(),
82 Attributes: make(map[int]interface{}),
83 groupMembership: make(map[string]bool),
84 sessionID: uid,
85 }
86 }
87
88 // WithKeytab sets the Keytab in the Credentials struct.
89 func (c *Credentials) WithKeytab(kt keytab.Keytab) *Credentials {
90 c.Keytab = kt
91 return c
92 }
93
94 // WithPassword sets the password in the Credentials struct.
95 func (c *Credentials) WithPassword(password string) *Credentials {
96 c.Password = password
97 return c
98 }
99
100 // HasKeytab queries if the Credentials has a keytab defined.
101 func (c *Credentials) HasKeytab() bool {
102 if len(c.Keytab.Entries) > 0 {
103 return true
104 }
105 return false
106 }
107
108 // SetValidUntil sets the expiry time of the credentials
109 func (c *Credentials) SetValidUntil(t time.Time) {
110 c.ValidUntil = t
111 }
112
113 // HasPassword queries if the Credentials has a password defined.
114 func (c *Credentials) HasPassword() bool {
115 if c.Password != "" {
116 return true
117 }
118 return false
119 }
120
121 // SetADCredentials adds ADCredentials attributes to the credentials
122 func (c *Credentials) SetADCredentials(a ADCredentials) {
123 c.Attributes[AttributeKeyADCredentials] = a
124 if a.FullName != "" {
125 c.SetDisplayName(a.FullName)
126 }
127 if a.EffectiveName != "" {
128 c.SetUserName(a.EffectiveName)
129 }
130 if a.LogonDomainName != "" {
131 c.SetDomain(a.LogonDomainName)
132 }
133 for i := range a.GroupMembershipSIDs {
134 c.AddAuthzAttribute(a.GroupMembershipSIDs[i])
135 }
136 }
137
138 // Methods to implement goidentity.Identity interface
139
140 // UserName returns the credential's username.
141 func (c *Credentials) UserName() string {
142 return c.Username
143 }
144
145 // SetUserName sets the username value on the credential.
146 func (c *Credentials) SetUserName(s string) {
147 c.Username = s
148 }
149
150 // Domain returns the credential's domain.
151 func (c *Credentials) Domain() string {
152 return c.Realm
153 }
154
155 // SetDomain sets the domain value on the credential.
156 func (c *Credentials) SetDomain(s string) {
157 c.Realm = s
158 }
159
160 // DisplayName returns the credential's display name.
161 func (c *Credentials) DisplayName() string {
162 return c.displayName
163 }
164
165 // SetDisplayName sets the display name value on the credential.
166 func (c *Credentials) SetDisplayName(s string) {
167 c.displayName = s
168 }
169
170 // Human returns if the credential represents a human or not.
171 func (c *Credentials) Human() bool {
172 return c.human
173 }
174
175 // SetHuman sets the credential as human.
176 func (c *Credentials) SetHuman(b bool) {
177 c.human = b
178 }
179
180 // AuthTime returns the time the credential was authenticated.
181 func (c *Credentials) AuthTime() time.Time {
182 return c.authTime
183 }
184
185 // SetAuthTime sets the time the credential was authenticated.
186 func (c *Credentials) SetAuthTime(t time.Time) {
187 c.authTime = t
188 }
189
190 // AuthzAttributes returns the credentials authorizing attributes.
191 func (c *Credentials) AuthzAttributes() []string {
192 s := make([]string, len(c.groupMembership))
193 i := 0
194 for a := range c.groupMembership {
195 s[i] = a
196 i++
197 }
198 return s
199 }
200
201 // Authenticated indicates if the credential has been successfully authenticated or not.
202 func (c *Credentials) Authenticated() bool {
203 return c.authenticated
204 }
205
206 // SetAuthenticated sets the credential as having been successfully authenticated.
207 func (c *Credentials) SetAuthenticated(b bool) {
208 c.authenticated = b
209 }
210
211 // AddAuthzAttribute adds an authorization attribute to the credential.
212 func (c *Credentials) AddAuthzAttribute(a string) {
213 c.groupMembership[a] = true
214 }
215
216 // RemoveAuthzAttribute removes an authorization attribute from the credential.
217 func (c *Credentials) RemoveAuthzAttribute(a string) {
218 if _, ok := c.groupMembership[a]; !ok {
219 return
220 }
221 delete(c.groupMembership, a)
222 }
223
224 // EnableAuthzAttribute toggles an authorization attribute to an enabled state on the credential.
225 func (c *Credentials) EnableAuthzAttribute(a string) {
226 if enabled, ok := c.groupMembership[a]; ok && !enabled {
227 c.groupMembership[a] = true
228 }
229 }
230
231 // DisableAuthzAttribute toggles an authorization attribute to a disabled state on the credential.
232 func (c *Credentials) DisableAuthzAttribute(a string) {
233 if enabled, ok := c.groupMembership[a]; ok && enabled {
234 c.groupMembership[a] = false
235 }
236 }
237
238 // Authorized indicates if the credential has the specified authorizing attribute.
239 func (c *Credentials) Authorized(a string) bool {
240 if enabled, ok := c.groupMembership[a]; ok && enabled {
241 return true
242 }
243 return false
244 }
245
246 // SessionID returns the credential's session ID.
247 func (c *Credentials) SessionID() string {
248 return c.sessionID
249 }
250
251 // Expired indicates if the credential has expired.
252 func (c *Credentials) Expired() bool {
253 if !c.ValidUntil.IsZero() && time.Now().UTC().After(c.ValidUntil) {
254 return true
255 }
256 return false
257 }