kesavand | 2cde658 | 2020-06-22 04:56:23 -0400 | [diff] [blame^] | 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.v7/iana/nametype" |
| 9 | "gopkg.in/jcmturner/gokrb5.v7/keytab" |
| 10 | "gopkg.in/jcmturner/gokrb5.v7/types" |
| 11 | ) |
| 12 | |
| 13 | const ( |
| 14 | // AttributeKeyADCredentials assigned number for AD credentials. |
| 15 | AttributeKeyADCredentials = "gokrb5AttributeKeyADCredentials" |
| 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[string]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 | // New creates a new Credentials instance. |
| 54 | func New(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.New(), |
| 65 | attributes: make(map[string]interface{}), |
| 66 | groupMembership: make(map[string]bool), |
| 67 | sessionID: uid, |
| 68 | human: true, |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | // NewFromPrincipalName creates a new Credentials instance with the user details provides as a PrincipalName type. |
| 73 | func NewFromPrincipalName(cname types.PrincipalName, realm string) *Credentials { |
| 74 | uid, err := uuid.GenerateUUID() |
| 75 | if err != nil { |
| 76 | uid = "00unique-sess-ions-uuid-unavailable0" |
| 77 | } |
| 78 | return &Credentials{ |
| 79 | username: cname.PrincipalNameString(), |
| 80 | displayName: cname.PrincipalNameString(), |
| 81 | realm: realm, |
| 82 | cname: cname, |
| 83 | keytab: keytab.New(), |
| 84 | attributes: make(map[string]interface{}), |
| 85 | groupMembership: make(map[string]bool), |
| 86 | sessionID: uid, |
| 87 | human: true, |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | // WithKeytab sets the Keytab in the Credentials struct. |
| 92 | func (c *Credentials) WithKeytab(kt *keytab.Keytab) *Credentials { |
| 93 | c.keytab = kt |
| 94 | c.password = "" |
| 95 | return c |
| 96 | } |
| 97 | |
| 98 | // Keytab returns the credential's Keytab. |
| 99 | func (c *Credentials) Keytab() *keytab.Keytab { |
| 100 | return c.keytab |
| 101 | } |
| 102 | |
| 103 | // HasKeytab queries if the Credentials has a keytab defined. |
| 104 | func (c *Credentials) HasKeytab() bool { |
| 105 | if c.keytab != nil && len(c.keytab.Entries) > 0 { |
| 106 | return true |
| 107 | } |
| 108 | return false |
| 109 | } |
| 110 | |
| 111 | // WithPassword sets the password in the Credentials struct. |
| 112 | func (c *Credentials) WithPassword(password string) *Credentials { |
| 113 | c.password = password |
| 114 | c.keytab = keytab.New() // clear any keytab |
| 115 | return c |
| 116 | } |
| 117 | |
| 118 | // Password returns the credential's password. |
| 119 | func (c *Credentials) Password() string { |
| 120 | return c.password |
| 121 | } |
| 122 | |
| 123 | // HasPassword queries if the Credentials has a password defined. |
| 124 | func (c *Credentials) HasPassword() bool { |
| 125 | if c.password != "" { |
| 126 | return true |
| 127 | } |
| 128 | return false |
| 129 | } |
| 130 | |
| 131 | // SetValidUntil sets the expiry time of the credentials |
| 132 | func (c *Credentials) SetValidUntil(t time.Time) { |
| 133 | c.validUntil = t |
| 134 | } |
| 135 | |
| 136 | // SetADCredentials adds ADCredentials attributes to the credentials |
| 137 | func (c *Credentials) SetADCredentials(a ADCredentials) { |
| 138 | c.SetAttribute(AttributeKeyADCredentials, a) |
| 139 | if a.FullName != "" { |
| 140 | c.SetDisplayName(a.FullName) |
| 141 | } |
| 142 | if a.EffectiveName != "" { |
| 143 | c.SetUserName(a.EffectiveName) |
| 144 | } |
| 145 | for i := range a.GroupMembershipSIDs { |
| 146 | c.AddAuthzAttribute(a.GroupMembershipSIDs[i]) |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | // Methods to implement goidentity.Identity interface |
| 151 | |
| 152 | // UserName returns the credential's username. |
| 153 | func (c *Credentials) UserName() string { |
| 154 | return c.username |
| 155 | } |
| 156 | |
| 157 | // SetUserName sets the username value on the credential. |
| 158 | func (c *Credentials) SetUserName(s string) { |
| 159 | c.username = s |
| 160 | } |
| 161 | |
| 162 | // CName returns the credential's client principal name. |
| 163 | func (c *Credentials) CName() types.PrincipalName { |
| 164 | return c.cname |
| 165 | } |
| 166 | |
| 167 | // SetCName sets the client principal name on the credential. |
| 168 | func (c *Credentials) SetCName(pn types.PrincipalName) { |
| 169 | c.cname = pn |
| 170 | } |
| 171 | |
| 172 | // Domain returns the credential's domain. |
| 173 | func (c *Credentials) Domain() string { |
| 174 | return c.realm |
| 175 | } |
| 176 | |
| 177 | // SetDomain sets the domain value on the credential. |
| 178 | func (c *Credentials) SetDomain(s string) { |
| 179 | c.realm = s |
| 180 | } |
| 181 | |
| 182 | // Realm returns the credential's realm. Same as the domain. |
| 183 | func (c *Credentials) Realm() string { |
| 184 | return c.Domain() |
| 185 | } |
| 186 | |
| 187 | // SetRealm sets the realm value on the credential. Same as the domain |
| 188 | func (c *Credentials) SetRealm(s string) { |
| 189 | c.SetDomain(s) |
| 190 | } |
| 191 | |
| 192 | // DisplayName returns the credential's display name. |
| 193 | func (c *Credentials) DisplayName() string { |
| 194 | return c.displayName |
| 195 | } |
| 196 | |
| 197 | // SetDisplayName sets the display name value on the credential. |
| 198 | func (c *Credentials) SetDisplayName(s string) { |
| 199 | c.displayName = s |
| 200 | } |
| 201 | |
| 202 | // Human returns if the credential represents a human or not. |
| 203 | func (c *Credentials) Human() bool { |
| 204 | return c.human |
| 205 | } |
| 206 | |
| 207 | // SetHuman sets the credential as human. |
| 208 | func (c *Credentials) SetHuman(b bool) { |
| 209 | c.human = b |
| 210 | } |
| 211 | |
| 212 | // AuthTime returns the time the credential was authenticated. |
| 213 | func (c *Credentials) AuthTime() time.Time { |
| 214 | return c.authTime |
| 215 | } |
| 216 | |
| 217 | // SetAuthTime sets the time the credential was authenticated. |
| 218 | func (c *Credentials) SetAuthTime(t time.Time) { |
| 219 | c.authTime = t |
| 220 | } |
| 221 | |
| 222 | // AuthzAttributes returns the credentials authorizing attributes. |
| 223 | func (c *Credentials) AuthzAttributes() []string { |
| 224 | s := make([]string, len(c.groupMembership)) |
| 225 | i := 0 |
| 226 | for a := range c.groupMembership { |
| 227 | s[i] = a |
| 228 | i++ |
| 229 | } |
| 230 | return s |
| 231 | } |
| 232 | |
| 233 | // Authenticated indicates if the credential has been successfully authenticated or not. |
| 234 | func (c *Credentials) Authenticated() bool { |
| 235 | return c.authenticated |
| 236 | } |
| 237 | |
| 238 | // SetAuthenticated sets the credential as having been successfully authenticated. |
| 239 | func (c *Credentials) SetAuthenticated(b bool) { |
| 240 | c.authenticated = b |
| 241 | } |
| 242 | |
| 243 | // AddAuthzAttribute adds an authorization attribute to the credential. |
| 244 | func (c *Credentials) AddAuthzAttribute(a string) { |
| 245 | c.groupMembership[a] = true |
| 246 | } |
| 247 | |
| 248 | // RemoveAuthzAttribute removes an authorization attribute from the credential. |
| 249 | func (c *Credentials) RemoveAuthzAttribute(a string) { |
| 250 | if _, ok := c.groupMembership[a]; !ok { |
| 251 | return |
| 252 | } |
| 253 | delete(c.groupMembership, a) |
| 254 | } |
| 255 | |
| 256 | // EnableAuthzAttribute toggles an authorization attribute to an enabled state on the credential. |
| 257 | func (c *Credentials) EnableAuthzAttribute(a string) { |
| 258 | if enabled, ok := c.groupMembership[a]; ok && !enabled { |
| 259 | c.groupMembership[a] = true |
| 260 | } |
| 261 | } |
| 262 | |
| 263 | // DisableAuthzAttribute toggles an authorization attribute to a disabled state on the credential. |
| 264 | func (c *Credentials) DisableAuthzAttribute(a string) { |
| 265 | if enabled, ok := c.groupMembership[a]; ok && enabled { |
| 266 | c.groupMembership[a] = false |
| 267 | } |
| 268 | } |
| 269 | |
| 270 | // Authorized indicates if the credential has the specified authorizing attribute. |
| 271 | func (c *Credentials) Authorized(a string) bool { |
| 272 | if enabled, ok := c.groupMembership[a]; ok && enabled { |
| 273 | return true |
| 274 | } |
| 275 | return false |
| 276 | } |
| 277 | |
| 278 | // SessionID returns the credential's session ID. |
| 279 | func (c *Credentials) SessionID() string { |
| 280 | return c.sessionID |
| 281 | } |
| 282 | |
| 283 | // Expired indicates if the credential has expired. |
| 284 | func (c *Credentials) Expired() bool { |
| 285 | if !c.validUntil.IsZero() && time.Now().UTC().After(c.validUntil) { |
| 286 | return true |
| 287 | } |
| 288 | return false |
| 289 | } |
| 290 | |
| 291 | // Attributes returns the Credentials' attributes map. |
| 292 | func (c *Credentials) Attributes() map[string]interface{} { |
| 293 | return c.attributes |
| 294 | } |
| 295 | |
| 296 | // SetAttribute sets the value of an attribute. |
| 297 | func (c *Credentials) SetAttribute(k string, v interface{}) { |
| 298 | c.attributes[k] = v |
| 299 | } |
| 300 | |
| 301 | // SetAttributes replaces the attributes map with the one provided. |
| 302 | func (c *Credentials) SetAttributes(a map[string]interface{}) { |
| 303 | c.attributes = a |
| 304 | } |
| 305 | |
| 306 | // RemoveAttribute deletes an attribute from the attribute map that has the key provided. |
| 307 | func (c *Credentials) RemoveAttribute(k string) { |
| 308 | delete(c.attributes, k) |
| 309 | } |