-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.go
79 lines (60 loc) · 2.26 KB
/
model.go
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
76
77
78
79
package cassh
import (
"fmt"
"time"
)
// Username of the CASSH user.
type Username string
// String implements stringer for Username.
func (u Username) String() string { return string(u) }
// UserStatus stores the status attributes of a CASSH user.
type UserStatus struct {
Name Username
RealName string
KeyState KeyState
KeyExpiration time.Time
KeyPrincipals Principals
}
// String implements stringer for UserStatus.
func (us UserStatus) String() string {
return fmt.Sprintf("[%s] %s (%s)", us.KeyState.String(), us.Name.String(), us.RealName)
}
// KeyState defines the different states a user key can be in.
type KeyState string
const (
// KeyStateActive means the key is usable on the CASSH server.
KeyStateActive KeyState = "ACTIVE"
// KeyStateRevoked means the key has been revoked by the CASSH server and cannot be used anymore.
KeyStateRevoked KeyState = "REVOKED"
// KeyStatePending means the key has not been signed yet by a CASSH server admin and cannot be used yet.
KeyStatePending KeyState = "PENDING"
)
// String implements stringer for KeyState.
func (ks KeyState) String() string { return string(ks) }
// Principals aliases []Principal to add useful methods.
type Principals []Principal
// Has returns whenever provided principals exists all in the list of principals.
func (principals Principals) Has(requiredPrincipal Principal, requiredPrincipals ...Principal) error {
requiredPrincipals = append([]Principal{requiredPrincipal}, requiredPrincipals...)
for _, requiredPrincipal := range requiredPrincipals {
var found bool
for _, principal := range principals {
if principal == requiredPrincipal {
found = true
break
}
}
if !found {
return fmt.Errorf("%s not found in list of principals", requiredPrincipal)
}
}
return nil
}
// Principal stores a single principal.
type Principal string
// String implements stringer for Principal.
func (principal Principal) String() string { return string(principal) }
type sentinelError string
func (err sentinelError) Error() string { return string(err) }
// ErrInsufficientPrivileges is returned when the privileges provided to the CASSH server are not sufficient to execute the request successfully.
const ErrInsufficientPrivileges = sentinelError("insufficient privileges")