-
Notifications
You must be signed in to change notification settings - Fork 3
/
auth.go
80 lines (66 loc) · 2.31 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
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
80
/* Copyright (c) Fortanix, Inc.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
package sdkms
import (
"context"
"encoding/base64"
"net/http"
)
// Authorization provides an HTTP authorization header
type Authorization interface {
setAuthorization(*http.Header)
}
// APIKey is used for app authentication
type APIKey string
func (a APIKey) setAuthorization(header *http.Header) {
header.Set("Authorization", "Basic "+string(a))
}
// BasicAuth is used for user authentication
type BasicAuth struct {
Username string
Password string
}
func (a BasicAuth) setAuthorization(header *http.Header) {
v := base64.StdEncoding.EncodeToString([]byte(a.Username + ":" + a.Password))
header.Set("Authorization", "Basic "+v)
}
// BearerToken is used for established sessions
type BearerToken string
func (a BearerToken) setAuthorization(header *http.Header) {
header.Set("Authorization", "Bearer "+string(a))
}
// AuthenticationResponse is the response returned by AuthenticateWith* APIs
type AuthenticationResponse struct {
ExpiresIn int32 `json:"expires_in"`
AccessToken string `json:"access_token"`
EntityID string `json:"entity_id"`
}
func (c *Client) authenticate(ctx context.Context, auth Authorization) (*AuthenticationResponse, error) {
var response AuthenticationResponse
err := c.fetchWithAuth(ctx, http.MethodPost, "/sys/v1/session/auth", nil, &response, auth)
if err != nil {
return nil, err
}
c.Auth = BearerToken(response.AccessToken)
return &response, nil
}
// AuthenticateWithUserPass authenticates a user
func (c *Client) AuthenticateWithUserPass(ctx context.Context, username, password string) (*AuthenticationResponse, error) {
return c.authenticate(ctx, BasicAuth{Username: username, Password: password})
}
// AuthenticateWithAPIKey authenticates an app
func (c *Client) AuthenticateWithAPIKey(ctx context.Context, apiKey string) (*AuthenticationResponse, error) {
return c.authenticate(ctx, APIKey(apiKey))
}
// TerminateSession terminates the current session
func (c *Client) TerminateSession(ctx context.Context) error {
err := c.fetch(ctx, http.MethodPost, "/sys/v1/session/terminate", nil, nil)
if err != nil {
return err
}
c.Auth = nil
return nil
}