go-spectre/scope.go

76 lines
1.4 KiB
Go

package spectre
import (
"fmt"
"strings"
)
// Interface guard
var _ Scoper = (*SimpleScoper)(nil)
// Scope is a key scope
type Scope string
const (
Authentication Scope = "Authentication"
Identification Scope = "Identification"
Recovery Scope = "Recovery"
)
// DefaultTemplate is the default Template for a Scope
func (s Scope) DefaultTemplate() Template {
switch s {
case Identification:
return Name
case Recovery:
return Phrase
case Authentication:
fallthrough
default:
return Long
}
}
// ParseScope returns a Scope from s
func ParseScope(s string) (Scope, error) {
switch strings.ToLower(s) {
case "authentication", "a":
return Authentication, nil
case "identification", "i":
return Identification, nil
case "recovery", "r":
return Recovery, nil
default:
return "", fmt.Errorf("unknown Scope %q; available [authentication, identification, recovery]", s)
}
}
// Scoper returns one of the three available scopes
type Scoper interface {
Scope(Scope) string
}
// SimpleScoper is a simple Scoper
type SimpleScoper struct {
Key string
}
// Scope fulfills Scoper
func (s SimpleScoper) Scope(scope Scope) string {
switch scope {
case Identification:
return s.Key + ".login"
case Recovery:
return s.Key + ".answer"
case Authentication:
fallthrough
default:
return s.Key
}
}
// DefaultScoper is the default Scoper
var DefaultScoper = SimpleScoper{
Key: "com.lyndir.masterpassword",
}