forked from remind101/empire
-
Notifications
You must be signed in to change notification settings - Fork 0
/
authorization.go
54 lines (41 loc) · 1.28 KB
/
authorization.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
package authorization
import (
"errors"
"fmt"
"github.com/remind101/empire/empire"
)
var (
// ErrTwoFactor is returned by an Authorizer when a two factor code is
// either invalid or required.
ErrTwoFactor = errors.New("authorization: two factor code required or invalid")
// ErrUnauthorized is returned when creating an authorization fails
// because of invalid credentials.
ErrUnauthorized = errors.New("authorization: unauthorized")
)
// Authorizer is an interface that can authorize a user.
type Authorizer interface {
Authorize(username, password, twofactor string) (*empire.User, error)
}
// Fake is a fake implementation of the Authorizer interface that let's
// anyone in. Used in development and tests.
type Fake struct{}
// Authorizer implements Authorizer Authorize.
func (a *Fake) Authorize(username, password, twofactor string) (*empire.User, error) {
user := &empire.User{Name: "fake", GitHubToken: "token"}
if username == "fake" {
return user, nil
}
if username == "twofactor" {
if twofactor == "code" {
return user, nil
}
return nil, ErrTwoFactor
}
return nil, ErrUnauthorized
}
type MembershipError struct {
Organization string
}
func (e *MembershipError) Error() string {
return fmt.Sprintf("authorization: not a member of %s", e.Organization)
}