Files
threshold/internal/auth/subtype.go
T

86 lines
2.1 KiB
Go
Raw Normal View History

package auth
import (
"strings"
2021-04-08 20:54:32 -04:00
"github.com/hashicorp/boundary/internal/auth/oidc"
"github.com/hashicorp/boundary/internal/auth/password"
2021-04-08 20:54:32 -04:00
"github.com/hashicorp/boundary/internal/db/timestamp"
)
type Subtype int
const (
UnknownSubtype Subtype = iota
PasswordSubtype
2021-04-08 20:54:32 -04:00
OidcSubtype
)
func (t Subtype) String() string {
switch t {
case PasswordSubtype:
return "password"
2021-04-08 20:54:32 -04:00
case OidcSubtype:
return "oidc"
}
return "unknown"
}
2021-04-08 20:54:32 -04:00
// AuthMethod contains the common methods across all the different types of auth methods.
type AuthMethod interface {
GetPublicId() string
GetCreateTime() *timestamp.Timestamp
GetUpdateTime() *timestamp.Timestamp
GetName() string
GetDescription() string
GetScopeId() string
GetVersion() uint32
GetIsPrimaryAuthMethod() bool
}
var (
_ AuthMethod = (*oidc.AuthMethod)(nil)
_ AuthMethod = (*password.AuthMethod)(nil)
)
type Account interface {
GetPublicId() string
GetCreateTime() *timestamp.Timestamp
GetUpdateTime() *timestamp.Timestamp
GetName() string
GetDescription() string
GetAuthMethodId() string
GetVersion() uint32
}
var (
_ Account = (*oidc.Account)(nil)
_ Account = (*password.Account)(nil)
)
// SubtypeFromType converts a string to a Subtype.
// returns UnknownSubtype if no Subtype with that name is found.
func SubtypeFromType(t string) Subtype {
switch {
case strings.EqualFold(strings.TrimSpace(t), PasswordSubtype.String()):
return PasswordSubtype
2021-04-08 20:54:32 -04:00
case strings.EqualFold(strings.TrimSpace(t), OidcSubtype.String()):
return OidcSubtype
}
return UnknownSubtype
}
// SubtypeFromId takes any public id in the auth subsystem and uses the prefix to determine
// what subtype the id is for.
// Returns UnknownSubtype if no Subtype with this id's prefix is found.
func SubtypeFromId(id string) Subtype {
switch {
case strings.HasPrefix(strings.TrimSpace(id), password.AuthMethodPrefix),
strings.HasPrefix(strings.TrimSpace(id), password.AccountPrefix):
return PasswordSubtype
2021-04-08 20:54:32 -04:00
case strings.HasPrefix(strings.TrimSpace(id), oidc.AuthMethodPrefix),
strings.HasPrefix(strings.TrimSpace(id), oidc.AccountPrefix):
return OidcSubtype
}
return UnknownSubtype
}