Skip to content
This repository was archived by the owner on Aug 1, 2023. It is now read-only.

Commit f6e2926

Browse files
author
hzlouchao
committed
add validate user's token method for v2 and bug fix for reauth
1 parent 63ee53d commit f6e2926

File tree

5 files changed

+35
-1
lines changed

5 files changed

+35
-1
lines changed

openstack/client.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,7 @@ func v3auth(client *gophercloud.ProviderClient, endpoint string, options gopherc
167167

168168
if options.AllowReauth {
169169
client.ReauthFunc = func() error {
170+
client.TokenID = ""
170171
return AuthenticateV3(client, options)
171172
}
172173
}

openstack/identity/v2/tokens/requests.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,3 +88,15 @@ func Create(client *gophercloud.ServiceClient, auth AuthOptionsBuilder) CreateRe
8888
})
8989
return result
9090
}
91+
92+
// Validates and retrieves information for user's token.
93+
func Get(client *gophercloud.ServiceClient, token string) GetResult {
94+
var result GetResult
95+
_, result.Err = client.Get(CreateGetURL(client, token), &result.Body, &gophercloud.RequestOpts{
96+
OkCodes: []int{200, 203},
97+
})
98+
if result.Err != nil {
99+
return result
100+
}
101+
return result
102+
}

openstack/identity/v2/tokens/results.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,11 @@ type CreateResult struct {
7474
gophercloud.Result
7575
}
7676

77+
// GetResult is the deferred response from a Get call.
78+
type GetResult struct {
79+
gophercloud.Result
80+
}
81+
7782
// ExtractToken returns the just-created Token from a CreateResult.
7883
func (result CreateResult) ExtractToken() (*Token, error) {
7984
if result.Err != nil {

openstack/identity/v2/tokens/urls.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,8 @@ import "github.com/rackspace/gophercloud"
66
func CreateURL(client *gophercloud.ServiceClient) string {
77
return client.ServiceURL("tokens")
88
}
9+
10+
// CreateGetURL generates the URL used to Validate Tokens.
11+
func CreateGetURL(client *gophercloud.ServiceClient, token string) string {
12+
return client.ServiceURL("tokens", token)
13+
}

provider_client.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,10 +185,21 @@ func (client *ProviderClient) Request(method, url string, options RequestOpts) (
185185

186186
if resp.StatusCode == http.StatusUnauthorized {
187187
if client.ReauthFunc != nil {
188-
err = client.ReauthFunc()
188+
// make sure ReauthFunc only exec one time, or will occur endless recursive loop when admin reauth fail
189+
execFunc := client.ReauthFunc
190+
client.ReauthFunc = nil
191+
err = execFunc()
192+
client.ReauthFunc = execFunc
189193
if err != nil {
190194
return nil, fmt.Errorf("Error trying to re-authenticate: %s", err)
191195
}
196+
197+
if options.MoreHeaders != nil {
198+
options.MoreHeaders["X-Auth-Token"] = client.TokenID
199+
} else {
200+
options.MoreHeaders = client.AuthenticatedHeaders()
201+
}
202+
192203
if options.RawBody != nil {
193204
options.RawBody.Seek(0, 0)
194205
}

0 commit comments

Comments
 (0)