-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
client_builder_auth.go
39 lines (31 loc) · 1.27 KB
/
client_builder_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
package fastshot
import (
"encoding/base64"
"github.com/opus-domini/fast-shot/constant/header"
)
// BuilderAuth is the interface that wraps the basic methods for setting authentication configurations.
var _ BuilderAuth[ClientBuilder] = (*ClientAuthBuilder)(nil)
// ClientAuthBuilder allows for setting authentication configurations.
type ClientAuthBuilder struct {
parentBuilder *ClientBuilder
}
// Auth BuilderAuth returns a new ClientAuthBuilder for setting authentication options.
func (b *ClientBuilder) Auth() *ClientAuthBuilder {
return &ClientAuthBuilder{parentBuilder: b}
}
// Set sets the Authorization header for custom authentication.
func (b *ClientAuthBuilder) Set(value string) *ClientBuilder {
b.parentBuilder.client.Header().Set(header.Authorization, value)
return b.parentBuilder
}
// BearerToken sets the Authorization header for Bearer token authentication.
func (b *ClientAuthBuilder) BearerToken(token string) *ClientBuilder {
b.Set("Bearer " + token)
return b.parentBuilder
}
// BasicAuth sets the Authorization header for Basic authentication.
func (b *ClientAuthBuilder) BasicAuth(username, password string) *ClientBuilder {
encoded := base64.StdEncoding.EncodeToString([]byte(username + ":" + password))
b.Set("Basic " + encoded)
return b.parentBuilder
}