@@ -3,79 +3,14 @@ package entraid
3
3
import (
4
4
"context"
5
5
"crypto"
6
- "errors "
6
+ "crypto/x509 "
7
7
"fmt"
8
- confidential "github.com/AzureAD/microsoft-authentication-library-for-go/apps/confidential"
9
- )
8
+ "time"
10
9
11
- import "crypto/x509"
12
-
13
- const (
14
- // AuthorityTypeDefault is the default authority type.
15
- // This is used to specify the authority type when requesting a token.
16
- AuthorityTypeDefault = "default"
17
- // AuthorityTypeMultiTenant is the multi-tenant authority type.
18
- // This is used to specify the multi-tenant authority type when requesting a token.
19
- // This type of authority is used to authenticate the identity when requesting a token.
20
- AuthorityTypeMultiTenant = "multi-tenant"
21
- // AuthorityTypeCustom is the custom authority type.
22
- // This is used to specify the custom authority type when requesting a token.
23
- AuthorityTypeCustom = "custom"
10
+ confidential "github.com/AzureAD/microsoft-authentication-library-for-go/apps/confidential"
24
11
)
25
12
26
- type AuthorityConfiguration struct {
27
- // AuthorityType is the type of authority used to authenticate with the identity provider.
28
- // This can be either "default", "multi-tenant", or "custom".
29
- AuthorityType string
30
-
31
- // Authority is the authority used to authenticate with the identity provider.
32
- // This is typically the URL of the identity provider.
33
- // For example, "https://login.microsoftonline.com/{tenantID}/v2.0"
34
- Authority string
35
-
36
- // TenantID is the tenant ID of the identity provider.
37
- // This is used to identify the tenant when requesting a token.
38
- // This is typically the ID of the Azure Active Directory tenant.
39
- TenantID string
40
- }
41
-
42
- func (a AuthorityConfiguration ) GetAuthority () (string , error ) {
43
- if a .AuthorityType == "" {
44
- a .AuthorityType = AuthorityTypeDefault
45
- }
46
-
47
- switch a .AuthorityType {
48
- case AuthorityTypeDefault :
49
- return "https://login.microsoftonline.com/common" , nil
50
- case AuthorityTypeMultiTenant :
51
- if a .TenantID == "" {
52
- return "" , errors .New ("tenant ID is required when using multi-tenant authority type" )
53
- }
54
- return fmt .Sprintf ("https://login.microsoftonline.com/%s" , a .TenantID ), nil
55
- case AuthorityTypeCustom :
56
- if a .Authority == "" {
57
- return "" , errors .New ("authority is required when using custom authority type" )
58
- }
59
- return a .Authority , nil
60
- default :
61
- return "" , errors .New ("invalid authority type" )
62
- }
63
- }
64
-
65
- type ConfidentialIdentityProvider struct {
66
- // clientID is the client ID used to authenticate with the identity provider.
67
- clientID string
68
-
69
- // credential is the credential used to authenticate with the identity provider.
70
- credential confidential.Credential
71
-
72
- // scopes is the list of scopes used to request a token from the identity provider.
73
- scopes []string
74
-
75
- // client confidential is the client used to request a token from the identity provider.
76
- client * confidential.Client
77
- }
78
-
13
+ // ConfidentialIdentityProviderOptions represents the options for the confidential identity provider.
79
14
type ConfidentialIdentityProviderOptions struct {
80
15
// ClientID is the client ID used to authenticate with the identity provider.
81
16
ClientID string
@@ -99,21 +34,42 @@ type ConfidentialIdentityProviderOptions struct {
99
34
Authority AuthorityConfiguration
100
35
}
101
36
37
+ // ConfidentialIdentityProvider represents a confidential identity provider.
38
+ type ConfidentialIdentityProvider struct {
39
+ // clientID is the client ID used to authenticate with the identity provider.
40
+ clientID string
41
+
42
+ // credential is the credential used to authenticate with the identity provider.
43
+ credential confidential.Credential
44
+
45
+ // scopes is the list of scopes used to request a token from the identity provider.
46
+ scopes []string
47
+
48
+ // client confidential is the client used to request a token from the identity provider.
49
+ client * confidential.Client
50
+ }
51
+
52
+ // NewConfidentialIdentityProvider creates a new confidential identity provider.
53
+ // It is used to configure the identity provider when requesting a token.
54
+ // It is used to specify the client ID, tenant ID, and scopes for the identity.
55
+ // It is also used to specify the type of credentials used to authenticate with the identity provider.
56
+ // The credentials can be either a client secret or a client certificate.
57
+ // The authority is used to authenticate with the identity provider.
102
58
func NewConfidentialIdentityProvider (opts ConfidentialIdentityProviderOptions ) (* ConfidentialIdentityProvider , error ) {
103
59
var credential confidential.Credential
104
60
var authority string
105
61
var err error
106
62
107
63
if opts .ClientID == "" {
108
- return nil , errors . New ("client ID is required" )
64
+ return nil , fmt . Errorf ("client ID is required" )
109
65
}
110
66
111
67
if opts .CredentialsType != ClientSecretCredentialType && opts .CredentialsType != ClientCertificateCredentialType {
112
- return nil , errors . New ("invalid credentials type" )
68
+ return nil , fmt . Errorf ("invalid credentials type" )
113
69
}
114
70
115
71
// Get the authority from the authority configuration.
116
- authority , err = opts .Authority .GetAuthority ()
72
+ authority , err = opts .Authority .getAuthority ()
117
73
if err != nil {
118
74
return nil , fmt .Errorf ("failed to get authority: %w" , err )
119
75
}
@@ -122,7 +78,7 @@ func NewConfidentialIdentityProvider(opts ConfidentialIdentityProviderOptions) (
122
78
case ClientSecretCredentialType :
123
79
// ClientSecretCredentialType is the type of credentials that uses a client secret to authenticate.
124
80
if opts .ClientSecret == "" {
125
- return nil , errors . New ("client secret is required when using client secret credentials" )
81
+ return nil , fmt . Errorf ("client secret is required when using client secret credentials" )
126
82
}
127
83
128
84
credential , err = confidential .NewCredFromSecret (opts .ClientSecret )
@@ -132,10 +88,10 @@ func NewConfidentialIdentityProvider(opts ConfidentialIdentityProviderOptions) (
132
88
case ClientCertificateCredentialType :
133
89
// ClientCertificateCredentialType is the type of credentials that uses a client certificate to authenticate.
134
90
if opts .ClientCert == nil {
135
- return nil , errors . New ("client certificate is required when using client certificate credentials" )
91
+ return nil , fmt . Errorf ("client certificate is required when using client certificate credentials" )
136
92
}
137
93
if opts .ClientPrivateKey == nil {
138
- return nil , errors . New ("client private key is required when using client certificate credentials" )
94
+ return nil , fmt . Errorf ("client private key is required when using client certificate credentials" )
139
95
}
140
96
credential , err = confidential .NewCredFromCert (opts .ClientCert , opts .ClientPrivateKey )
141
97
if err != nil {
@@ -160,15 +116,18 @@ func NewConfidentialIdentityProvider(opts ConfidentialIdentityProviderOptions) (
160
116
}, nil
161
117
}
162
118
163
- func (c * ConfidentialIdentityProvider ) RequestToken () (string , error ) {
119
+ // RequestToken requests a token from the identity provider.
120
+ // It returns the token, the expiration time, and an error if any.
121
+ // The token is used to authenticate the identity when requesting a token.
122
+ func (c * ConfidentialIdentityProvider ) RequestToken () (string , time.Time , error ) {
164
123
if c .client == nil {
165
- return "" , errors . New ("client is not initialized" )
124
+ return "" , time. Time {}, fmt . Errorf ("client is not initialized" )
166
125
}
167
126
168
127
result , err := c .client .AcquireTokenByCredential (context .TODO (), c .scopes )
169
128
if err != nil {
170
- return "" , fmt .Errorf ("failed to acquire token: %w" , err )
129
+ return "" , time. Time {}, fmt .Errorf ("failed to acquire token: %w" , err )
171
130
}
172
131
173
- return result .AccessToken , nil
132
+ return result .AccessToken , result . ExpiresOn . UTC (), nil
174
133
}
0 commit comments