59 lines
1.2 KiB
Go
59 lines
1.2 KiB
Go
package models
|
|
|
|
import (
|
|
"crypto/sha256"
|
|
"encoding/base64"
|
|
|
|
"go.infratographer.com/x/gidx"
|
|
)
|
|
|
|
const (
|
|
MetalUserIssuer = "https://auth.equinix.com/"
|
|
MetalUserIssuerIDPrefix = "auth|"
|
|
)
|
|
|
|
type UserDetails struct {
|
|
id *gidx.PrefixedID
|
|
ID string `json:"id"`
|
|
FullName string `json:"full_name"`
|
|
Organizations []*OrganizationDetails `json:"organizations"`
|
|
Projects []*ProjectDetails `json:"projects"`
|
|
Roles []string `json:"roles"`
|
|
}
|
|
|
|
func (d *UserDetails) PrefixedID() gidx.PrefixedID {
|
|
if d.id != nil {
|
|
return *d.id
|
|
}
|
|
|
|
nullID := gidx.NullPrefixedID
|
|
|
|
d.id = &nullID
|
|
|
|
if d.ID == "" {
|
|
return nullID
|
|
}
|
|
|
|
id, err := GenerateSubjectID(IdentityPrefixUser, MetalUserIssuer, MetalUserIssuerIDPrefix+d.ID)
|
|
if err != nil {
|
|
return nullID
|
|
}
|
|
|
|
d.id = &id
|
|
|
|
return *d.id
|
|
}
|
|
|
|
func GenerateSubjectID(prefix, iss, sub string) (gidx.PrefixedID, error) {
|
|
// Concatenate the iss and sub values, then hash them
|
|
issSub := iss + sub
|
|
issSubHash := sha256.Sum256([]byte(issSub))
|
|
|
|
digest := base64.RawURLEncoding.EncodeToString(issSubHash[:])
|
|
|
|
// Concatenate the prefix with the digest
|
|
out := prefix + "-" + digest
|
|
|
|
return gidx.Parse(out)
|
|
}
|