|
1 | 1 | package entraid
|
2 | 2 |
|
3 |
| -import "time" |
| 3 | +import ( |
| 4 | + "time" |
| 5 | +) |
4 | 6 |
|
5 | 7 | // Token represents the authentication token used to access the Entraid API.
|
6 | 8 | // It contains the username, password, expiration time, time to live, and the raw token.
|
7 | 9 | // The token is used to authenticate the user and authorize access to the API.
|
8 | 10 | // The token is typically obtained from an identity provider and is used to access the Entraid API.
|
9 | 11 | // The token is valid for a limited time and must be refreshed periodically.
|
10 | 12 | type Token struct {
|
11 |
| - // Username is the username of the user. |
12 |
| - Username string `json:"username"` |
13 |
| - // Password is the password of the user. |
14 |
| - Password string `json:"password"` |
15 |
| - // ExpiresOn is the expiration time of the token. |
16 |
| - ExpiresOn time.Time `json:"expires_on"` |
17 |
| - // TTL is the time to live of the token. |
18 |
| - TTL int64 `json:"ttl"` |
19 |
| - // RawToken is the authentication token. |
20 |
| - RawToken string `json:"raw_token"` |
| 13 | + // username is the username of the user. |
| 14 | + username string `json:"username"` |
| 15 | + // password is the password of the user. |
| 16 | + password string `json:"password"` |
| 17 | + // expiresOn is the expiration time of the token. |
| 18 | + expiresOn time.Time `json:"expires_on"` |
| 19 | + // ttl is the time to live of the token. |
| 20 | + ttl int64 `json:"ttl"` |
| 21 | + // rawToken is the authentication token. |
| 22 | + rawToken string `json:"raw_token"` |
| 23 | + // receivedAt is the time when the token was received. |
| 24 | + receivedAt time.Time `json:"received_at"` |
21 | 25 | }
|
22 | 26 |
|
23 |
| -// IdentityProviderResponseParserFunc is a function that parses the token and returns the username and password. |
24 |
| -type IdentityProviderResponseParserFunc func(response IdentityProviderResponse) (*Token, error) |
| 27 | +// BasicAuth returns the username and password for basic authentication. |
| 28 | +// It implements the auth.Credentials interface. |
| 29 | +func (t *Token) BasicAuth() (string, string) { |
| 30 | + return t.username, t.password |
| 31 | +} |
25 | 32 |
|
26 |
| -// copyToken creates a copy of the token. |
27 |
| -func copyToken(token *Token) *Token { |
| 33 | +// RawCredentials returns the raw credentials for authentication. |
| 34 | +// It implements the auth.Credentials interface. |
| 35 | +func (t *Token) RawCredentials() string { |
| 36 | + return t.rawToken |
| 37 | +} |
| 38 | + |
| 39 | +// IsExpired checks if the token is expired. |
| 40 | +// It returns true if the token is expired, false otherwise. |
| 41 | +func (t *Token) IsExpired() bool { |
| 42 | + return t.expiresOn.Before(time.Now()) |
| 43 | +} |
| 44 | + |
| 45 | +// IsValid checks if the token is valid. |
| 46 | +// It returns true if the token is valid, false otherwise. |
| 47 | +func (t *Token) IsValid() bool { |
| 48 | + return !t.IsExpired() |
| 49 | +} |
| 50 | + |
| 51 | +// ExpirationOn returns the expiration time of the token. |
| 52 | +func (t *Token) ExpirationOn() time.Time { |
| 53 | + return t.expiresOn |
| 54 | +} |
| 55 | + |
| 56 | +// NewToken creates a new token with the specified username, password, raw token, expiration time, received at time, and time to live. |
| 57 | +func NewToken(username, password, rawToken string, expiresOn, receivedAt time.Time, ttl int64) *Token { |
28 | 58 | return &Token{
|
29 |
| - Username: token.Username, |
30 |
| - Password: token.Password, |
31 |
| - ExpiresOn: token.ExpiresOn, |
32 |
| - TTL: token.TTL, |
33 |
| - RawToken: token.RawToken, |
| 59 | + username: username, |
| 60 | + password: password, |
| 61 | + expiresOn: expiresOn, |
| 62 | + receivedAt: receivedAt, |
| 63 | + ttl: ttl, |
| 64 | + rawToken: rawToken, |
34 | 65 | }
|
35 | 66 | }
|
| 67 | + |
| 68 | +// copyToken creates a copy of the token. |
| 69 | +func copyToken(token *Token) *Token { |
| 70 | + return NewToken(token.username, token.password, token.rawToken, token.expiresOn, token.receivedAt, token.ttl) |
| 71 | +} |
| 72 | + |
| 73 | +// compareCredentials two tokens if they are the same credentials |
| 74 | +func (t *Token) compareCredentials(token *Token) bool { |
| 75 | + return t.username == token.username && t.password == token.password |
| 76 | +} |
| 77 | + |
| 78 | +// compareRawCredentials two tokens if they are the same raw credentials |
| 79 | +func (t *Token) compareRawCredentials(token *Token) bool { |
| 80 | + return t.rawToken == token.rawToken |
| 81 | +} |
| 82 | + |
| 83 | +// compareToken compares two tokens if they are the same token |
| 84 | +func (t *Token) compareToken(token *Token) bool { |
| 85 | + return t.compareCredentials(token) && t.compareRawCredentials(token) |
| 86 | +} |
0 commit comments