Home

go-spectre @main - refs - log -
-
https://git.jolheiser.com/go-spectre.git
Go implementation for spectre/masterpassword
go-spectre / scope.go
- raw
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
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",
}