-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauthentication.go
More file actions
47 lines (41 loc) · 1.43 KB
/
authentication.go
File metadata and controls
47 lines (41 loc) · 1.43 KB
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
package jmap
import (
"errors"
"net/http"
"strings"
)
// Authenticator signs outgoing HTTP requests, e.g. by adding an Authorization
// header. Implement this interface to support custom authentication schemes.
type Authenticator interface {
// Authenticate mutates req to add authentication credentials.
Authenticate(req *http.Request) error
}
// WithBearerTokenAuthentication is a convenience [ClientOpt] that creates a
// [BearerTokenAuthenticator] from the given token and sets it on the client.
func WithBearerTokenAuthentication(token string) ClientOpt {
return func(c *Client) error {
var err error
c.authn, err = NewBearerTokenAuthenticator(token)
return err
}
}
// NewBearerTokenAuthenticator returns a [BearerTokenAuthenticator] for the
// given token. Returns an error if the token is empty or whitespace-only.
func NewBearerTokenAuthenticator(token string) (*BearerTokenAuthenticator, error) {
if strings.TrimSpace(token) == "" {
return nil, errors.New("jmap: bearer token must not be empty")
}
return &BearerTokenAuthenticator{
token: token,
}, nil
}
// BearerTokenAuthenticator implements [Authenticator] using a static Bearer
// token. Use [NewBearerTokenAuthenticator] or [WithBearerTokenAuthentication]
// to construct one.
type BearerTokenAuthenticator struct {
token string
}
func (b *BearerTokenAuthenticator) Authenticate(req *http.Request) error {
req.Header.Set("Authorization", "Bearer "+b.token)
return nil
}