-
Notifications
You must be signed in to change notification settings - Fork 1
/
auth.go
51 lines (42 loc) · 1.28 KB
/
auth.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
package lib
import (
"context"
"errors"
"strings"
auth3 "github.com/BytemarkHosting/auth-client"
)
// AuthWithCredentials attempts to authenticate with the given credentials. Returns nil on success or an error otherwise.
func (c *bytemarkClient) AuthWithCredentials(credentials auth3.Credentials) error {
session, err := c.auth.CreateSession(context.TODO(), credentials)
if err == nil {
c.authSession = session
}
return err
}
// AuthWithToken attempts to read sessiondata from auth for the given token. Returns nil on success or an error otherwise.
func (c *bytemarkClient) AuthWithToken(token string) error {
if token == "" {
return errors.New("No token provided")
}
if strings.HasPrefix(token, "apikey.") {
c.authSession = &auth3.SessionData{
Username: "apikeyuser",
Factors: []string{
"apikey",
},
Token: token,
}
c.urls.Billing = ""
return nil
}
session, err := c.auth.ReadSession(context.TODO(), token)
if err == nil {
c.authSession = session
}
return err
}
// Impersonate creates a session for the given user (assuming the client has already authenticated as someone who can)
func (c *bytemarkClient) Impersonate(user string) (err error) {
c.authSession, err = c.auth.CreateImpersonatedSession(context.TODO(), c.authSession.Token, user)
return
}