Skip to content

Commit

Permalink
Fix check if non expiry token (#1652)
Browse files Browse the repository at this point in the history
  • Loading branch information
kushalmalani committed May 16, 2024
1 parent a8d64c9 commit 8ea319f
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
2 changes: 1 addition & 1 deletion cmd/cloud/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,7 @@ func checkAPIToken(isDeploymentFile bool, platformCoreClient astroplatformcore.C
if len(claims.Permissions) == 0 {
return false, errNotAPIToken
}
if claims.ExpiresAt.Before(time.Now()) {
if claims.ExpiresAt != nil && claims.ExpiresAt.Before(time.Now()) {
fmt.Printf("The given API Token %s has expired \n", claims.APITokenID)
return false, errExpiredAPIToken
}
Expand Down
40 changes: 40 additions & 0 deletions cmd/cloud/setup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -460,4 +460,44 @@ func TestCheckAPIToken(t *testing.T) {
_, err = checkAPIToken(true, mockPlatformCoreClient)
assert.ErrorIs(t, err, errExpiredAPIToken)
})

t.Run("non-expiry token", func(t *testing.T) {
permissions = []string{
"workspaceId:workspace-id",
"organizationId:org-ID",
}
mockClaims = util.CustomClaims{
Permissions: permissions,
RegisteredClaims: jwt.RegisteredClaims{
Issuer: "test-issuer",
Subject: "test-subject",
Audience: jwt.ClaimStrings{"audience1", "audience2"},
NotBefore: jwt.NewNumericDate(time.Now()),
IssuedAt: jwt.NewNumericDate(time.Now()),
ID: "test-id",
},
}

authLogin = func(domain, token string, coreClient astrocore.CoreClient, platformCoreClient astroplatformcore.CoreClient, out io.Writer, shouldDisplayLoginLink bool) error {
return nil
}

parseAPIToken = func(astroAPIToken string) (*util.CustomClaims, error) {
return &mockClaims, nil
}

mockPlatformCoreClient.On("ListOrganizationsWithResponse", mock.Anything, &astroplatformcore.ListOrganizationsParams{}).Return(&mockOrgsResponse, nil).Once()

t.Setenv("ASTRO_API_TOKEN", "token")

// Switch context
domain := "astronomer-dev.io"
err := context.Switch(domain)
assert.NoError(t, err)

// run CheckAPIKeys
_, err = checkAPIToken(true, mockPlatformCoreClient)
assert.NoError(t, err)
})

}

0 comments on commit 8ea319f

Please sign in to comment.