-
Notifications
You must be signed in to change notification settings - Fork 1
/
authentication.go
68 lines (58 loc) · 1.97 KB
/
authentication.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
/*
Package advancedbilling
This file was automatically generated for Maxio by APIMATIC v3.0 ( https://www.apimatic.io ).
*/
package advancedbilling
import (
"errors"
"github.com/apimatic/go-core-runtime/https"
"net/http"
)
// BasicAuthCredentials represents the credentials required for `BasicAuth` authentication.
type BasicAuthCredentials struct {
username string
password string
}
// NewBasicAuthCredentials creates a new instance of BasicAuthCredentials with provided parameters.
func NewBasicAuthCredentials(
username string,
password string) BasicAuthCredentials {
return BasicAuthCredentials {
username: username,
password: password,
}
}
// WithUsername sets username in BasicAuthCredentials.
func (b BasicAuthCredentials) WithUsername(username string) BasicAuthCredentials {
b.username = username
return b
}
// WithPassword sets password in BasicAuthCredentials.
func (b BasicAuthCredentials) WithPassword(password string) BasicAuthCredentials {
b.password = password
return b
}
// Username returns the username associated with the BasicAuthCredentials.
func (b BasicAuthCredentials) Username() string {
return b.username
}
// Password returns the password associated with the BasicAuthCredentials.
func (b BasicAuthCredentials) Password() string {
return b.password
}
// Validate function returns validation error associated with the BasicAuthCredentials.
func (b BasicAuthCredentials) Validate() error {
if b.username == "" || b.password == "" {
return errors.New("BasicAuth : missing auth credentials -> Username && Password.")
}
return nil
}
// Authenticator function returns HttpInterceptor function that provides authentication for API calls.
func (b BasicAuthCredentials) Authenticator() https.HttpInterceptor {
return func(req *http.Request,
next https.HttpCallExecutor,
) https.HttpContext {
req.SetBasicAuth(b.Username(), b.Password())
return next(req)
}
}