convert metlusr to idntusr

This commit is contained in:
Mike Mason
2023-07-17 17:17:04 +00:00
parent cb0651b7bf
commit 02ac2870bc
3 changed files with 65 additions and 7 deletions

View File

@@ -1,12 +1,19 @@
package models
import "go.infratographer.com/x/gidx"
import (
"crypto/sha256"
"encoding/base64"
"go.infratographer.com/x/gidx"
)
const (
MetalUserPrefix = "metlusr"
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"`
@@ -15,9 +22,37 @@ type UserDetails struct {
}
func (d *UserDetails) PrefixedID() gidx.PrefixedID {
if d.ID == "" {
return gidx.NullPrefixedID
if d.id != nil {
return *d.id
}
return gidx.PrefixedID(IDPrefixUser + "-" + 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)
}