Files
threshold/internal/auth/password/authmethod.go
T
Michael Gaffney 7897c1cf52 Add base resources for password authentication (#213)
* Add base SQL and protobuf files

* Add options and public Ids

* Add AuthMethod and Account resources

* Add repository

* Add CreateAuthMethod to repository

* Add CreateAccount method to repository

* Remove redundant foreign keys

* Document relationships between iam and auth tables

* Document relationships between auth and auth_password tables

* Add documentation for subtype triggers

* Use wt_scope_id domain type for scope_id

* Refactor: move test helper function

* Remove check from SetTableName

* Remove check from another SetTableName

* Use org scope in tests

* Remove handling of CreateTime and UpdateTime from docs
2020-07-24 10:23:35 -04:00

71 lines
1.7 KiB
Go

package password
import (
"fmt"
"github.com/hashicorp/watchtower/internal/auth/password/store"
"github.com/hashicorp/watchtower/internal/db"
"github.com/hashicorp/watchtower/internal/oplog"
"google.golang.org/protobuf/proto"
)
// A AuthMethod contains accounts and password configurations. It is owned
// by a scope.
type AuthMethod struct {
*store.AuthMethod
tableName string
}
// NewAuthMethod creates a new in memory AuthMethod assigned to scopeId.
// Name and description are the only valid options. All other options are
// ignored.
func NewAuthMethod(scopeId string, opt ...Option) (*AuthMethod, error) {
if scopeId == "" {
return nil, fmt.Errorf("new: password auth method: no scope id: %w", db.ErrInvalidParameter)
}
opts := getOpts(opt...)
a := &AuthMethod{
AuthMethod: &store.AuthMethod{
ScopeId: scopeId,
Name: opts.withName,
Description: opts.withDescription,
MinUserNameLength: 5,
MinPasswordLength: 8,
},
}
return a, nil
}
func (a *AuthMethod) clone() *AuthMethod {
cp := proto.Clone(a.AuthMethod)
return &AuthMethod{
AuthMethod: cp.(*store.AuthMethod),
}
}
// TableName returns the table name.
func (a *AuthMethod) TableName() string {
if a.tableName != "" {
return a.tableName
}
return "auth_password_method"
}
// SetTableName sets the table name.
func (a *AuthMethod) SetTableName(n string) {
a.tableName = n
}
func (a *AuthMethod) oplog(op oplog.OpType) oplog.Metadata {
metadata := oplog.Metadata{
"resource-public-id": []string{a.GetPublicId()},
"resource-type": []string{"password auth method"},
"op-type": []string{op.String()},
}
if a.ScopeId != "" {
metadata["scope-id"] = []string{a.ScopeId}
}
return metadata
}