2022-11-12 15:56:17 -05:00
|
|
|
package provider
|
|
|
|
|
2022-11-12 16:31:26 -05:00
|
|
|
import (
|
2022-11-12 17:31:03 -05:00
|
|
|
"net/http"
|
|
|
|
|
2022-11-20 08:49:24 -05:00
|
|
|
"dynatron.me/x/blasphem/pkg/flow"
|
2022-11-12 16:31:26 -05:00
|
|
|
"dynatron.me/x/blasphem/pkg/storage"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Constructor func(storage.Store) (AuthProvider, error)
|
|
|
|
|
|
|
|
var Providers = make(map[string]Constructor)
|
|
|
|
|
2022-11-12 15:56:17 -05:00
|
|
|
type AuthProvider interface { // TODO: this should include stepping
|
|
|
|
AuthProviderMetadata
|
|
|
|
ProviderBase() AuthProviderBase
|
2022-11-20 12:51:26 -05:00
|
|
|
FlowSchema() flow.Schema
|
2022-11-12 15:56:17 -05:00
|
|
|
NewCredData() interface{}
|
2022-11-12 17:31:03 -05:00
|
|
|
ValidateCreds(r *http.Request, reqMap map[string]interface{}) (user ProviderUser, success bool)
|
2022-11-13 19:06:53 -05:00
|
|
|
Lookup(ProviderUser) ProviderUser
|
2022-11-12 15:56:17 -05:00
|
|
|
}
|
|
|
|
|
2022-11-12 16:31:26 -05:00
|
|
|
func Register(providerName string, f func(storage.Store) (AuthProvider, error)) {
|
|
|
|
Providers[providerName] = f
|
|
|
|
}
|
|
|
|
|
2022-12-18 09:55:08 -05:00
|
|
|
type Credentials struct {
|
|
|
|
ID CredID `json:"id"`
|
|
|
|
UserID UserID `json:"user_id"`
|
|
|
|
AuthProviderType string `json:"auth_provider_type"`
|
|
|
|
AuthProviderID *string `json:"auth_provider_id"`
|
|
|
|
DataRaw *json.RawMessage `json:"data,omitempty"`
|
|
|
|
user ProviderUser `json:"-"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cred *Credentials) MarshalJSON() ([]byte, error) {
|
|
|
|
type CredAlias Credentials // alias so ø method set and we don't recurse
|
|
|
|
nCd := (*CredAlias)(cred)
|
|
|
|
|
|
|
|
if cred.user != nil {
|
|
|
|
providerData := cred.user.UserData()
|
|
|
|
if providerData != nil {
|
|
|
|
b, err := json.Marshal(providerData)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
dr := json.RawMessage(b)
|
|
|
|
nCd.DataRaw = &dr
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return json.Marshal(nCd)
|
|
|
|
}
|
|
|
|
|
2022-11-12 15:56:17 -05:00
|
|
|
type ProviderUser interface {
|
2022-11-20 08:49:24 -05:00
|
|
|
// TODO: make sure this is sane with all the ProviderUser and UserData type stuff
|
2022-11-13 11:55:10 -05:00
|
|
|
UserData() ProviderUser
|
2022-12-18 09:55:08 -05:00
|
|
|
Credentials() *Credentials
|
2022-11-12 15:56:17 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
type AuthProviderBase struct {
|
|
|
|
Name string `json:"name"`
|
|
|
|
ID *string `json:"id"`
|
|
|
|
Type string `json:"type"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type AuthProviderMetadata interface {
|
|
|
|
ProviderName() string
|
|
|
|
ProviderID() *string
|
|
|
|
ProviderType() string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (bp *AuthProviderBase) ProviderName() string { return bp.Name }
|
|
|
|
func (bp *AuthProviderBase) ProviderID() *string { return bp.ID }
|
|
|
|
func (bp *AuthProviderBase) ProviderType() string { return bp.Type }
|
|
|
|
func (bp *AuthProviderBase) ProviderBase() AuthProviderBase { return *bp }
|