github.com/gophercloud/gophercloud@v1.11.0/openstack/identity/v3/extensions/oauth1/results.go (about) 1 package oauth1 2 3 import ( 4 "encoding/json" 5 "net/url" 6 "time" 7 8 "github.com/gophercloud/gophercloud" 9 "github.com/gophercloud/gophercloud/pagination" 10 ) 11 12 // Consumer represents a delegated authorization request between two 13 // identities. 14 type Consumer struct { 15 ID string `json:"id"` 16 Secret string `json:"secret"` 17 Description string `json:"description"` 18 } 19 20 type consumerResult struct { 21 gophercloud.Result 22 } 23 24 // CreateConsumerResult is the response from a Create operation. Call its 25 // Extract method to interpret it as a Consumer. 26 type CreateConsumerResult struct { 27 consumerResult 28 } 29 30 // UpdateConsumerResult is the response from a Create operation. Call its 31 // Extract method to interpret it as a Consumer. 32 type UpdateConsumerResult struct { 33 consumerResult 34 } 35 36 // DeleteConsumerResult is the response from a Delete operation. Call its 37 // ExtractErr to determine if the request succeeded or failed. 38 type DeleteConsumerResult struct { 39 gophercloud.ErrResult 40 } 41 42 // ConsumersPage is a single page of Region results. 43 type ConsumersPage struct { 44 pagination.LinkedPageBase 45 } 46 47 // GetConsumerResult is the response from a Get operation. Call its Extract 48 // method to interpret it as a Consumer. 49 type GetConsumerResult struct { 50 consumerResult 51 } 52 53 // IsEmpty determines whether or not a page of Consumers contains any results. 54 func (c ConsumersPage) IsEmpty() (bool, error) { 55 if c.StatusCode == 204 { 56 return true, nil 57 } 58 59 consumers, err := ExtractConsumers(c) 60 return len(consumers) == 0, err 61 } 62 63 // NextPageURL extracts the "next" link from the links section of the result. 64 func (c ConsumersPage) NextPageURL() (string, error) { 65 var s struct { 66 Links struct { 67 Next string `json:"next"` 68 Previous string `json:"previous"` 69 } `json:"links"` 70 } 71 err := c.ExtractInto(&s) 72 if err != nil { 73 return "", err 74 } 75 return s.Links.Next, err 76 } 77 78 // ExtractConsumers returns a slice of Consumers contained in a single page of 79 // results. 80 func ExtractConsumers(r pagination.Page) ([]Consumer, error) { 81 var s struct { 82 Consumers []Consumer `json:"consumers"` 83 } 84 err := (r.(ConsumersPage)).ExtractInto(&s) 85 return s.Consumers, err 86 } 87 88 // Extract interprets any consumer result as a Consumer. 89 func (c consumerResult) Extract() (*Consumer, error) { 90 var s struct { 91 Consumer *Consumer `json:"consumer"` 92 } 93 err := c.ExtractInto(&s) 94 return s.Consumer, err 95 } 96 97 // Token contains an OAuth1 token. 98 type Token struct { 99 // OAuthToken is the key value for the oauth token that the Identity API returns. 100 OAuthToken string `q:"oauth_token"` 101 // OAuthTokenSecret is the secret value associated with the OAuth Token. 102 OAuthTokenSecret string `q:"oauth_token_secret"` 103 // OAuthExpiresAt is the date and time when an OAuth token expires. 104 OAuthExpiresAt *time.Time `q:"-"` 105 } 106 107 // TokenResult is a struct to handle 108 // "Content-Type: application/x-www-form-urlencoded" response. 109 type TokenResult struct { 110 gophercloud.Result 111 Body []byte 112 } 113 114 // Extract interprets any OAuth1 token result as a Token. 115 func (r TokenResult) Extract() (*Token, error) { 116 if r.Err != nil { 117 return nil, r.Err 118 } 119 120 values, err := url.ParseQuery(string(r.Body)) 121 if err != nil { 122 return nil, err 123 } 124 125 token := &Token{ 126 OAuthToken: values.Get("oauth_token"), 127 OAuthTokenSecret: values.Get("oauth_token_secret"), 128 } 129 130 if v := values.Get("oauth_expires_at"); v != "" { 131 if t, err := time.Parse(gophercloud.RFC3339Milli, v); err != nil { 132 return nil, err 133 } else { 134 token.OAuthExpiresAt = &t 135 } 136 } 137 138 return token, nil 139 } 140 141 // AuthorizedToken contains an OAuth1 authorized token info. 142 type AuthorizedToken struct { 143 // OAuthVerifier is the ID of the token verifier. 144 OAuthVerifier string `json:"oauth_verifier"` 145 } 146 147 type AuthorizeTokenResult struct { 148 gophercloud.Result 149 } 150 151 // Extract interprets AuthorizeTokenResult result as a AuthorizedToken. 152 func (r AuthorizeTokenResult) Extract() (*AuthorizedToken, error) { 153 var s struct { 154 AuthorizedToken *AuthorizedToken `json:"token"` 155 } 156 err := r.ExtractInto(&s) 157 return s.AuthorizedToken, err 158 } 159 160 // AccessToken represents an AccessToken response as a struct. 161 type AccessToken struct { 162 ID string `json:"id"` 163 ConsumerID string `json:"consumer_id"` 164 ProjectID string `json:"project_id"` 165 AuthorizingUserID string `json:"authorizing_user_id"` 166 ExpiresAt *time.Time `json:"-"` 167 } 168 169 func (r *AccessToken) UnmarshalJSON(b []byte) error { 170 type tmp AccessToken 171 var s struct { 172 tmp 173 ExpiresAt *gophercloud.JSONRFC3339Milli `json:"expires_at"` 174 } 175 err := json.Unmarshal(b, &s) 176 if err != nil { 177 return err 178 } 179 *r = AccessToken(s.tmp) 180 181 if s.ExpiresAt != nil { 182 t := time.Time(*s.ExpiresAt) 183 r.ExpiresAt = &t 184 } 185 186 return nil 187 } 188 189 type GetAccessTokenResult struct { 190 gophercloud.Result 191 } 192 193 // Extract interprets any GetAccessTokenResult result as an AccessToken. 194 func (r GetAccessTokenResult) Extract() (*AccessToken, error) { 195 var s struct { 196 AccessToken *AccessToken `json:"access_token"` 197 } 198 err := r.ExtractInto(&s) 199 return s.AccessToken, err 200 } 201 202 // RevokeAccessTokenResult is the response from a Delete operation. Call its 203 // ExtractErr to determine if the request succeeded or failed. 204 type RevokeAccessTokenResult struct { 205 gophercloud.ErrResult 206 } 207 208 // AccessTokensPage is a single page of Access Tokens results. 209 type AccessTokensPage struct { 210 pagination.LinkedPageBase 211 } 212 213 // IsEmpty determines whether or not a an AccessTokensPage contains any results. 214 func (r AccessTokensPage) IsEmpty() (bool, error) { 215 if r.StatusCode == 204 { 216 return true, nil 217 } 218 219 accessTokens, err := ExtractAccessTokens(r) 220 return len(accessTokens) == 0, err 221 } 222 223 // NextPageURL extracts the "next" link from the links section of the result. 224 func (r AccessTokensPage) NextPageURL() (string, error) { 225 var s struct { 226 Links struct { 227 Next string `json:"next"` 228 Previous string `json:"previous"` 229 } `json:"links"` 230 } 231 err := r.ExtractInto(&s) 232 if err != nil { 233 return "", err 234 } 235 return s.Links.Next, err 236 } 237 238 // ExtractAccessTokens returns a slice of AccessTokens contained in a single 239 // page of results. 240 func ExtractAccessTokens(r pagination.Page) ([]AccessToken, error) { 241 var s struct { 242 AccessTokens []AccessToken `json:"access_tokens"` 243 } 244 err := (r.(AccessTokensPage)).ExtractInto(&s) 245 return s.AccessTokens, err 246 } 247 248 // AccessTokenRole represents an Access Token Role struct. 249 type AccessTokenRole struct { 250 ID string `json:"id"` 251 Name string `json:"name"` 252 DomainID string `json:"domain_id"` 253 } 254 255 // AccessTokenRolesPage is a single page of Access Token roles results. 256 type AccessTokenRolesPage struct { 257 pagination.LinkedPageBase 258 } 259 260 // IsEmpty determines whether or not a an AccessTokensPage contains any results. 261 func (r AccessTokenRolesPage) IsEmpty() (bool, error) { 262 if r.StatusCode == 204 { 263 return true, nil 264 } 265 266 accessTokenRoles, err := ExtractAccessTokenRoles(r) 267 return len(accessTokenRoles) == 0, err 268 } 269 270 // NextPageURL extracts the "next" link from the links section of the result. 271 func (r AccessTokenRolesPage) NextPageURL() (string, error) { 272 var s struct { 273 Links struct { 274 Next string `json:"next"` 275 Previous string `json:"previous"` 276 } `json:"links"` 277 } 278 err := r.ExtractInto(&s) 279 if err != nil { 280 return "", err 281 } 282 return s.Links.Next, err 283 } 284 285 // ExtractAccessTokenRoles returns a slice of AccessTokenRole contained in a 286 // single page of results. 287 func ExtractAccessTokenRoles(r pagination.Page) ([]AccessTokenRole, error) { 288 var s struct { 289 AccessTokenRoles []AccessTokenRole `json:"roles"` 290 } 291 err := (r.(AccessTokenRolesPage)).ExtractInto(&s) 292 return s.AccessTokenRoles, err 293 } 294 295 type GetAccessTokenRoleResult struct { 296 gophercloud.Result 297 } 298 299 // Extract interprets any GetAccessTokenRoleResult result as an AccessTokenRole. 300 func (r GetAccessTokenRoleResult) Extract() (*AccessTokenRole, error) { 301 var s struct { 302 AccessTokenRole *AccessTokenRole `json:"role"` 303 } 304 err := r.ExtractInto(&s) 305 return s.AccessTokenRole, err 306 } 307 308 // OAuth1 is an OAuth1 object, returned in OAuth1 token result. 309 type OAuth1 struct { 310 AccessTokenID string `json:"access_token_id"` 311 ConsumerID string `json:"consumer_id"` 312 } 313 314 // TokenExt represents an extension of the base token result. 315 type TokenExt struct { 316 OAuth1 OAuth1 `json:"OS-OAUTH1"` 317 }