Skip to content

Commit 8ff1c69

Browse files
ndyakovCopilot
andauthored
test(examples): Add runnable examples for testing (#2)
* update dependencies * add custom identity provider example * add additional examples * change client variable name * chore(exampels): Add runnable examples * fix(examples): fix run script * fix(tests): fix client configuration * chore(repo): use new repo under redis org * Update examples/entraid/config/config.go Co-authored-by: Copilot <[email protected]> * fix(examples): fix config imports * fix(examples): fix credentials type * wip(examples): debug cert example * wip(examples): use object id for user assigned * wip(examples): rename examples and add shorter expirations * fix(tests): remove examples from coverage trigger ci. * test(config): add test for the config parsing in the examples * test(examples): refactor parsePrivateKey to return err --------- Co-authored-by: Copilot <[email protected]>
1 parent 1e25b29 commit 8ff1c69

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+2054
-107
lines changed

.testcoverage.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ override:
3838
exclude:
3939
# Exclude files or packages matching their paths
4040
paths:
41+
- ^examples
4142
- \.pb\.go$ # excludes all protobuf generated files
4243
- ^pkg/bar # exclude package `pkg/bar`
4344

README.md

Lines changed: 46 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ import (
4444
"os"
4545
"strings"
4646

47-
"github.com/redis-developer/go-redis-entraid/entraid"
47+
"github.com/redis/go-redis-entraid/entraid"
4848
"github.com/redis/go-redis/v9"
4949
)
5050

@@ -97,7 +97,7 @@ export AZURE_AUTHORITY_HOST="https://login.microsoftonline.com" # For custom au
9797
### Running the Example
9898
```bash
9999
go mod init your-app
100-
go get github.com/redis-developer/go-redis-entraid
100+
go get github.com/redis/go-redis-entraid
101101
go run main.go
102102
```
103103

@@ -178,7 +178,7 @@ graph TD
178178
B -->|Yes| C{System Assigned?}
179179
B -->|No| D{Client Credentials?}
180180
C -->|Yes| E[SystemAssignedIdentity]
181-
C -->|No| F[UserAssignedIdentity]
181+
C -->|No| F[UserAssignedObjectID]
182182
D -->|Yes| G{Client Secret?}
183183
D -->|No| H[DefaultAzureIdentity]
184184
G -->|Yes| I[ClientSecret]
@@ -276,10 +276,10 @@ Options for managed identity authentication:
276276
```go
277277
type ManagedIdentityProviderOptions struct {
278278
// Required: Type of managed identity
279-
ManagedIdentityType ManagedIdentityType // SystemAssignedIdentity or UserAssignedIdentity
279+
ManagedIdentityType ManagedIdentityType // SystemAssignedIdentity or UserAssignedObjectID
280280

281281
// Optional: Client ID for user-assigned identity
282-
UserAssignedClientID string
282+
UserAssignedObjectID string
283283

284284
// Optional: Scopes for token access
285285
// Default: ["https://redis.azure.com/.default"]
@@ -411,10 +411,10 @@ authority := identity.AuthorityConfiguration{
411411
```go
412412
// Create provider for system assigned identity
413413
provider, err := entraid.NewManagedIdentityCredentialsProvider(entraid.ManagedIdentityCredentialsProviderOptions{
414-
CredentialsProviderOptions: entraid.CredentialsProviderOptions{
415-
ClientID: os.Getenv("AZURE_CLIENT_ID"),
414+
ManagedIdentityProviderOptions: identity.ManagedIdentityProviderOptions{
415+
ManagedIdentityType: identity.SystemAssignedIdentity,
416+
Scopes: []string{"https://redis.azure.com/.default"},
416417
},
417-
ManagedIdentityType: identity.SystemAssignedIdentity,
418418
})
419419
```
420420

@@ -425,23 +425,27 @@ provider, err := entraid.NewManagedIdentityCredentialsProvider(entraid.ManagedId
425425
CredentialsProviderOptions: entraid.CredentialsProviderOptions{
426426
ClientID: os.Getenv("AZURE_CLIENT_ID"),
427427
},
428-
ManagedIdentityType: identity.UserAssignedIdentity,
429-
UserAssignedClientID: os.Getenv("USER_ASSIGNED_CLIENT_ID"),
428+
ManagedIdentityProviderOptions: identity.ManagedIdentityProviderOptions{
429+
ManagedIdentityType: identity.UserAssignedObjectID,
430+
UserAssignedObjectID: os.Getenv("AZURE_USER_ASSIGNED_MANAGED_ID"),
431+
Scopes: []string{"https://redis.azure.com/.default"},
432+
},
430433
})
431434
```
432435

433436
### Client Secret Authentication
434437
```go
435438
// Create provider for client secret authentication
436-
provider, err := entraid.NewConfidentialCredentialsProvider(entraid.ConfidentialIdentityProviderOptions{
437-
CredentialsProviderOptions: entraid.CredentialsProviderOptions{
439+
provider, err := entraid.NewConfidentialCredentialsProvider(entraid.ConfidentialCredentialsProviderOptions{
440+
ConfidentialIdentityProviderOptions: identity.ConfidentialIdentityProviderOptions{
438441
ClientID: os.Getenv("AZURE_CLIENT_ID"),
439-
},
440-
CredentialsType: identity.ClientSecretCredentialType,
441-
ClientSecret: os.Getenv("AZURE_CLIENT_SECRET"),
442-
Authority: identity.AuthorityConfiguration{
443-
AuthorityType: identity.AuthorityTypeDefault,
444-
TenantID: os.Getenv("AZURE_TENANT_ID"),
442+
ClientSecret: os.Getenv("AZURE_CLIENT_SECRET"),
443+
CredentialsType: identity.ClientSecretCredentialType,
444+
Authority: identity.AuthorityConfiguration{
445+
AuthorityType: identity.AuthorityTypeMultiTenant,
446+
TenantID: os.Getenv("AZURE_TENANT_ID"),
447+
},
448+
Scopes: []string{"https://redis.azure.com/.default"},
445449
},
446450
})
447451
```
@@ -454,16 +458,27 @@ if err != nil {
454458
log.Fatal(err)
455459
}
456460

457-
provider, err := entraid.NewConfidentialCredentialsProvider(entraid.ConfidentialIdentityProviderOptions{
458-
CredentialsProviderOptions: entraid.CredentialsProviderOptions{
461+
provider, err := entraid.NewConfidentialCredentialsProvider(entraid.ConfidentialCredentialsProviderOptions{
462+
ConfidentialIdentityProviderOptions: identity.ConfidentialIdentityProviderOptions{
459463
ClientID: os.Getenv("AZURE_CLIENT_ID"),
464+
CredentialsType: identity.ClientCertificateCredentialType,
465+
Authority: identity.AuthorityConfiguration{
466+
AuthorityType: identity.AuthorityTypeMultiTenant,
467+
TenantID: os.Getenv("AZURE_TENANT_ID"),
468+
},
469+
Scopes: []string{"https://redis.azure.com/.default"},
470+
ClientCert: []*x509.Certificate{cert.Leaf},
471+
ClientPrivateKey: cert.PrivateKey,
460472
},
461-
CredentialsType: identity.ClientCertificateCredentialType,
462-
ClientCert: []*x509.Certificate{cert.Leaf},
463-
ClientPrivateKey: cert.PrivateKey,
464-
Authority: identity.AuthorityConfiguration{
465-
AuthorityType: identity.AuthorityTypeDefault,
466-
TenantID: os.Getenv("AZURE_TENANT_ID"),
473+
})
474+
```
475+
476+
### Default Azure Identity
477+
```go
478+
// Create a default credentials provider
479+
provider, err := entraid.NewDefaultAzureCredentialsProvider(entraid.DefaultAzureCredentialsProviderOptions{
480+
DefaultAzureIdentityProviderOptions: identity.DefaultAzureIdentityProviderOptions{
481+
Scopes: []string{"https://redis.azure.com/.default"},
467482
},
468483
})
469484
```
@@ -483,10 +498,10 @@ import (
483498
"strings"
484499
"time"
485500

486-
"github.com/redis-developer/go-redis-entraid/entraid"
487-
"github.com/redis-developer/go-redis-entraid/entraid/identity"
488-
"github.com/redis-developer/go-redis-entraid/entraid/manager"
489-
"github.com/redis-developer/go-redis-entraid/entraid/shared"
501+
"github.com/redis/go-redis-entraid/entraid"
502+
"github.com/redis/go-redis-entraid/entraid/identity"
503+
"github.com/redis/go-redis-entraid/entraid/manager"
504+
"github.com/redis/go-redis-entraid/entraid/shared"
490505
"github.com/redis/go-redis/v9"
491506
)
492507

@@ -855,7 +870,7 @@ The library provides several error types that you can check against:
855870

856871
```go
857872
// Import the shared package to access error types
858-
import "github.com/redis-developer/go-redis-entraid/shared"
873+
import "github.com/redis/go-redis-entraid/shared"
859874

860875
// Available error types:
861876
var (

credentials_provider.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ import (
77
"fmt"
88
"sync"
99

10-
"github.com/redis-developer/go-redis-entraid/manager"
11-
"github.com/redis-developer/go-redis-entraid/token"
10+
"github.com/redis/go-redis-entraid/manager"
11+
"github.com/redis/go-redis-entraid/token"
1212
"github.com/redis/go-redis/v9/auth"
1313
)
1414

credentials_provider_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ import (
55
"testing"
66
"time"
77

8-
"github.com/redis-developer/go-redis-entraid/identity"
9-
"github.com/redis-developer/go-redis-entraid/manager"
10-
"github.com/redis-developer/go-redis-entraid/shared"
11-
"github.com/redis-developer/go-redis-entraid/token"
8+
"github.com/redis/go-redis-entraid/identity"
9+
"github.com/redis/go-redis-entraid/manager"
10+
"github.com/redis/go-redis-entraid/shared"
11+
"github.com/redis/go-redis-entraid/token"
1212
"github.com/redis/go-redis/v9/auth"
1313
"github.com/stretchr/testify/assert"
1414
"github.com/stretchr/testify/mock"

entraid.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package entraid
22

3-
import "github.com/redis-developer/go-redis-entraid/shared"
3+
import "github.com/redis/go-redis-entraid/shared"
44

55
// IdentityProvider is an alias for the shared.IdentityProvider interface.
66
type IdentityProvider = shared.IdentityProvider

entraid_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ import (
77
"testing"
88
"time"
99

10-
"github.com/redis-developer/go-redis-entraid/manager"
11-
"github.com/redis-developer/go-redis-entraid/shared"
12-
"github.com/redis-developer/go-redis-entraid/token"
10+
"github.com/redis/go-redis-entraid/manager"
11+
"github.com/redis/go-redis-entraid/shared"
12+
"github.com/redis/go-redis-entraid/token"
1313
"github.com/redis/go-redis/v9/auth"
1414
"github.com/stretchr/testify/mock"
1515
)

examples/custom_idp/go.mod

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
module custom_example
2+
3+
go 1.23.4
4+
5+
require (
6+
github.com/redis/go-redis-entraid v0.0.0-20250415111332-9d087bc29c12
7+
github.com/redis/go-redis/v9 v9.5.3-0.20250415103233-40a89c56cc52
8+
)
9+
10+
require (
11+
github.com/Azure/azure-sdk-for-go/sdk/azcore v1.17.0 // indirect
12+
github.com/Azure/azure-sdk-for-go/sdk/azidentity v1.8.0-beta.1 // indirect
13+
github.com/Azure/azure-sdk-for-go/sdk/internal v1.10.0 // indirect
14+
github.com/AzureAD/microsoft-authentication-library-for-go v1.4.1 // indirect
15+
github.com/cespare/xxhash/v2 v2.3.0 // indirect
16+
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
17+
github.com/golang-jwt/jwt/v5 v5.2.1 // indirect
18+
github.com/google/uuid v1.6.0 // indirect
19+
github.com/kylelemons/godebug v1.1.0 // indirect
20+
github.com/pkg/browser v0.0.0-20240102092130-5ac0b6a4141c // indirect
21+
golang.org/x/crypto v0.33.0 // indirect
22+
golang.org/x/net v0.35.0 // indirect
23+
golang.org/x/sys v0.30.0 // indirect
24+
golang.org/x/text v0.22.0 // indirect
25+
)

examples/custom_idp/go.sum

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
github.com/Azure/azure-sdk-for-go/sdk/azcore v1.17.0 h1:g0EZJwz7xkXQiZAI5xi9f3WWFYBlX1CPTrR+NDToRkQ=
2+
github.com/Azure/azure-sdk-for-go/sdk/azcore v1.17.0/go.mod h1:XCW7KnZet0Opnr7HccfUw1PLc4CjHqpcaxW8DHklNkQ=
3+
github.com/Azure/azure-sdk-for-go/sdk/azidentity v1.8.0-beta.1 h1:iw4+KCeCoieuKodp1d5YhAa1TU/GgogCbw8RbGvsfLA=
4+
github.com/Azure/azure-sdk-for-go/sdk/azidentity v1.8.0-beta.1/go.mod h1:AP8cDnDTGIVvayqKAhwzpcAyTJosXpvLYNmVFJb98x8=
5+
github.com/Azure/azure-sdk-for-go/sdk/azidentity/cache v0.2.3 h1:BAUsn6/icUFtvUalVwCO0+hSF7qgU9DwwcEfCvtILtw=
6+
github.com/Azure/azure-sdk-for-go/sdk/azidentity/cache v0.2.3/go.mod h1:QlAsNp4gk9zLD2wiZIvIuv699ynpZ2Tq2ZBp+6MrSEw=
7+
github.com/Azure/azure-sdk-for-go/sdk/internal v1.10.0 h1:ywEEhmNahHBihViHepv3xPBn1663uRv2t2q/ESv9seY=
8+
github.com/Azure/azure-sdk-for-go/sdk/internal v1.10.0/go.mod h1:iZDifYGJTIgIIkYRNWPENUnqx6bJ2xnSDFI2tjwZNuY=
9+
github.com/AzureAD/microsoft-authentication-extensions-for-go/cache v0.1.1 h1:WJTmL004Abzc5wDB5VtZG2PJk5ndYDgVacGqfirKxjM=
10+
github.com/AzureAD/microsoft-authentication-extensions-for-go/cache v0.1.1/go.mod h1:tCcJZ0uHAmvjsVYzEFivsRTN00oz5BEsRgQHu5JZ9WE=
11+
github.com/AzureAD/microsoft-authentication-library-for-go v1.4.1 h1:8BKxhZZLX/WosEeoCvWysmKUscfa9v8LIPEEU0JjE2o=
12+
github.com/AzureAD/microsoft-authentication-library-for-go v1.4.1/go.mod h1:wP83P5OoQ5p6ip3ScPr0BAq0BvuPAvacpEuSzyouqAI=
13+
github.com/bsm/ginkgo/v2 v2.12.0 h1:Ny8MWAHyOepLGlLKYmXG4IEkioBysk6GpaRTLC8zwWs=
14+
github.com/bsm/ginkgo/v2 v2.12.0/go.mod h1:SwYbGRRDovPVboqFv0tPTcG1sN61LM1Z4ARdbAV9g4c=
15+
github.com/bsm/gomega v1.27.10 h1:yeMWxP2pV2fG3FgAODIY8EiRE3dy0aeFYt4l7wh6yKA=
16+
github.com/bsm/gomega v1.27.10/go.mod h1:JyEr/xRbxbtgWNi8tIEVPUYZ5Dzef52k01W3YH0H+O0=
17+
github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs=
18+
github.com/cespare/xxhash/v2 v2.3.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
19+
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
20+
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
21+
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/rVNCu3HqELle0jiPLLBs70cWOduZpkS1E78=
22+
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc=
23+
github.com/golang-jwt/jwt/v5 v5.2.1 h1:OuVbFODueb089Lh128TAcimifWaLhJwVflnrgM17wHk=
24+
github.com/golang-jwt/jwt/v5 v5.2.1/go.mod h1:pqrtFR0X4osieyHYxtmOUWsAWrfe1Q5UVIyoH402zdk=
25+
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
26+
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
27+
github.com/keybase/go-keychain v0.0.0-20231219164618-57a3676c3af6 h1:IsMZxCuZqKuao2vNdfD82fjjgPLfyHLpR41Z88viRWs=
28+
github.com/keybase/go-keychain v0.0.0-20231219164618-57a3676c3af6/go.mod h1:3VeWNIJaW+O5xpRQbPp0Ybqu1vJd/pm7s2F473HRrkw=
29+
github.com/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0SNc=
30+
github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw=
31+
github.com/pkg/browser v0.0.0-20240102092130-5ac0b6a4141c h1:+mdjkGKdHQG3305AYmdv1U2eRNDiU2ErMBj1gwrq8eQ=
32+
github.com/pkg/browser v0.0.0-20240102092130-5ac0b6a4141c/go.mod h1:7rwL4CYBLnjLxUqIJNnCWiEdr3bn6IUYi15bNlnbCCU=
33+
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
34+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
35+
github.com/redis/go-redis-entraid v0.0.0-20250415111332-9d087bc29c12 h1:H5ZfgueBAxs2eAvXtCMEbT2/fLQz/wxW5Ds4c0uzl50=
36+
github.com/redis/go-redis-entraid v0.0.0-20250415111332-9d087bc29c12/go.mod h1:uXKLxCMUAu1VKgWdt8gWc4PWCygiL2pAI5XpnRSVc0w=
37+
github.com/redis/go-redis/v9 v9.5.3-0.20250415103233-40a89c56cc52 h1:jRx2gINoJsGKxi/RYXCq1VneAAYes9JxUp13xH2oU2g=
38+
github.com/redis/go-redis/v9 v9.5.3-0.20250415103233-40a89c56cc52/go.mod h1:huWgSWd8mW6+m0VPhJjSSQ+d6Nh1VICQ6Q5lHuCH/Iw=
39+
github.com/stretchr/objx v0.5.2 h1:xuMeJ0Sdp5ZMRXx/aWO6RZxdr3beISkG5/G/aIRr3pY=
40+
github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA=
41+
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
42+
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
43+
golang.org/x/crypto v0.33.0 h1:IOBPskki6Lysi0lo9qQvbxiQ+FvsCC/YWOecCHAixus=
44+
golang.org/x/crypto v0.33.0/go.mod h1:bVdXmD7IV/4GdElGPozy6U7lWdRXA4qyRVGJV57uQ5M=
45+
golang.org/x/net v0.35.0 h1:T5GQRQb2y08kTAByq9L4/bz8cipCdA8FbRTXewonqY8=
46+
golang.org/x/net v0.35.0/go.mod h1:EglIi67kWsHKlRzzVMUD93VMSWGFOMSZgxFjparz1Qk=
47+
golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
48+
golang.org/x/sys v0.30.0 h1:QjkSwP/36a20jFYWkSue1YwXzLmsV5Gfq7Eiy72C1uc=
49+
golang.org/x/sys v0.30.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
50+
golang.org/x/text v0.22.0 h1:bofq7m3/HAFvbF51jz3Q9wLg3jkvSPuiZu/pD1XwgtM=
51+
golang.org/x/text v0.22.0/go.mod h1:YRoo4H8PVmsu+E3Ou7cqLVH8oXWIHVoX0jqUWALQhfY=
52+
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
53+
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

examples/custom_idp/main.go

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"strconv"
7+
"strings"
8+
"time"
9+
10+
entraid "github.com/redis/go-redis-entraid"
11+
"github.com/redis/go-redis-entraid/manager"
12+
"github.com/redis/go-redis-entraid/shared"
13+
"github.com/redis/go-redis-entraid/token"
14+
redis "github.com/redis/go-redis/v9"
15+
)
16+
17+
func main() {
18+
ctx := context.Background()
19+
idp := NewFakeIdentityProvider("local", "pass")
20+
parser := &fakeIdentityProviderResponseParser{}
21+
// create token manager
22+
23+
tm, err := manager.NewTokenManager(idp, manager.TokenManagerOptions{
24+
IdentityProviderResponseParser: parser,
25+
})
26+
27+
cp, err := entraid.NewCredentialsProvider(tm, entraid.CredentialsProviderOptions{})
28+
if err != nil {
29+
panic(err)
30+
}
31+
32+
client := redis.NewClient(&redis.Options{
33+
Addr: ":6379",
34+
StreamingCredentialsProvider: cp,
35+
})
36+
37+
ok, err := client.Ping(ctx).Result()
38+
if err != nil {
39+
panic(err)
40+
}
41+
fmt.Println("Ping result:", ok)
42+
}
43+
44+
var _ entraid.IdentityProvider = (*FakeIdentityProvider)(nil)
45+
46+
type FakeIdentityProvider struct {
47+
username string
48+
password string
49+
}
50+
51+
// RequestToken simulates a request to an identity provider and returns a fake token.
52+
// In a real implementation, this would involve making a network request to the identity provider.
53+
func (f *FakeIdentityProvider) RequestToken() (entraid.IdentityProviderResponse, error) {
54+
// Simulate a successful token request
55+
return shared.NewIDPResponse(
56+
shared.ResponseTypeRawToken,
57+
fmt.Sprintf("%s:%s:%d", f.username, f.password, time.Now().Add(1*time.Hour).Unix()),
58+
)
59+
}
60+
61+
// NewFakeIdentityProvider creates a new instance of FakeIdentityProvider with the given username and password.
62+
func NewFakeIdentityProvider(username, password string) *FakeIdentityProvider {
63+
return &FakeIdentityProvider{
64+
username: username,
65+
password: password,
66+
}
67+
}
68+
69+
type fakeIdentityProviderResponseParser struct {
70+
}
71+
72+
// ParseResponse simulates the parsing of a response from an identity provider.
73+
func (f *fakeIdentityProviderResponseParser) ParseResponse(response entraid.IdentityProviderResponse) (*token.Token, error) {
74+
if response.Type() == shared.ResponseTypeRawToken {
75+
rawToken := response.RawToken()
76+
username, password := "", ""
77+
var expiresOnUnix int64
78+
79+
// parse the raw token string
80+
// assuming the format is "username:password:expiresOnUnix"
81+
// where expiresOnUnix is a unix timestamp
82+
parts := strings.Split(rawToken, ":")
83+
if len(parts) != 3 {
84+
return nil, fmt.Errorf("invalid raw token format")
85+
}
86+
username = parts[0]
87+
password = parts[1]
88+
expiresOnUnix, err := strconv.ParseInt(parts[2], 10, 64)
89+
if err != nil {
90+
return nil, fmt.Errorf("failed to parse raw token: %w", err)
91+
}
92+
93+
// convert the unix timestamp to time.Time
94+
expiresOn := time.Unix(expiresOnUnix, 0)
95+
now := time.Now()
96+
return token.New(username, password, rawToken, expiresOn, now, int64(expiresOn.Sub(now).Seconds())), nil
97+
}
98+
return nil, fmt.Errorf("unsupported response type: %s", response.Type())
99+
}
100+
101+
var _ entraid.IdentityProviderResponseParser = (*fakeIdentityProviderResponseParser)(nil)

0 commit comments

Comments
 (0)