Ash Wilson | 85d8265 | 2014-08-28 13:57:46 -0400 | [diff] [blame] | 1 | package tokens |
| 2 | |
| 3 | import ( |
Ash Wilson | 85d8265 | 2014-08-28 13:57:46 -0400 | [diff] [blame] | 4 | "github.com/racker/perigee" |
| 5 | "github.com/rackspace/gophercloud" |
Ash Wilson | 85d8265 | 2014-08-28 13:57:46 -0400 | [diff] [blame] | 6 | ) |
| 7 | |
Ash Wilson | 85d8265 | 2014-08-28 13:57:46 -0400 | [diff] [blame] | 8 | // Scope allows a created token to be limited to a specific domain or project. |
| 9 | type Scope struct { |
| 10 | ProjectID string |
| 11 | ProjectName string |
| 12 | DomainID string |
| 13 | DomainName string |
| 14 | } |
| 15 | |
Ash Wilson | 6425a41 | 2014-08-29 12:30:35 -0400 | [diff] [blame] | 16 | func subjectTokenHeaders(c *gophercloud.ServiceClient, subjectToken string) map[string]string { |
Ash Wilson | 77857dc | 2014-10-22 09:09:02 -0400 | [diff] [blame] | 17 | h := c.AuthenticatedHeaders() |
Ash Wilson | 46d913f | 2014-08-29 11:00:11 -0400 | [diff] [blame] | 18 | h["X-Subject-Token"] = subjectToken |
| 19 | return h |
| 20 | } |
| 21 | |
Ash Wilson | e555086 | 2014-08-28 15:37:09 -0400 | [diff] [blame] | 22 | // Create authenticates and either generates a new token, or changes the Scope of an existing token. |
Ash Wilson | f8d546a | 2014-09-30 17:43:25 -0400 | [diff] [blame] | 23 | func Create(c *gophercloud.ServiceClient, options gophercloud.AuthOptions, scope *Scope) CreateResult { |
Ash Wilson | 85d8265 | 2014-08-28 13:57:46 -0400 | [diff] [blame] | 24 | type domainReq struct { |
| 25 | ID *string `json:"id,omitempty"` |
Ash Wilson | 417d922 | 2014-08-29 07:58:35 -0400 | [diff] [blame] | 26 | Name *string `json:"name,omitempty"` |
Ash Wilson | 85d8265 | 2014-08-28 13:57:46 -0400 | [diff] [blame] | 27 | } |
| 28 | |
| 29 | type projectReq struct { |
| 30 | Domain *domainReq `json:"domain,omitempty"` |
| 31 | Name *string `json:"name,omitempty"` |
| 32 | ID *string `json:"id,omitempty"` |
| 33 | } |
| 34 | |
| 35 | type userReq struct { |
| 36 | ID *string `json:"id,omitempty"` |
| 37 | Name *string `json:"name,omitempty"` |
| 38 | Password string `json:"password"` |
Ash Wilson | cde6812 | 2014-08-28 16:15:43 -0400 | [diff] [blame] | 39 | Domain *domainReq `json:"domain,omitempty"` |
Ash Wilson | 85d8265 | 2014-08-28 13:57:46 -0400 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | type passwordReq struct { |
| 43 | User userReq `json:"user"` |
| 44 | } |
| 45 | |
| 46 | type tokenReq struct { |
| 47 | ID string `json:"id"` |
| 48 | } |
| 49 | |
| 50 | type identityReq struct { |
| 51 | Methods []string `json:"methods"` |
Ash Wilson | cde6812 | 2014-08-28 16:15:43 -0400 | [diff] [blame] | 52 | Password *passwordReq `json:"password,omitempty"` |
Ash Wilson | 85d8265 | 2014-08-28 13:57:46 -0400 | [diff] [blame] | 53 | Token *tokenReq `json:"token,omitempty"` |
| 54 | } |
| 55 | |
| 56 | type scopeReq struct { |
| 57 | Domain *domainReq `json:"domain,omitempty"` |
| 58 | Project *projectReq `json:"project,omitempty"` |
| 59 | } |
| 60 | |
| 61 | type authReq struct { |
| 62 | Identity identityReq `json:"identity"` |
Ash Wilson | cde6812 | 2014-08-28 16:15:43 -0400 | [diff] [blame] | 63 | Scope *scopeReq `json:"scope,omitempty"` |
Ash Wilson | 85d8265 | 2014-08-28 13:57:46 -0400 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | type request struct { |
| 67 | Auth authReq `json:"auth"` |
| 68 | } |
| 69 | |
Ash Wilson | 85d8265 | 2014-08-28 13:57:46 -0400 | [diff] [blame] | 70 | // Populate the request structure based on the provided arguments. Create and return an error |
| 71 | // if insufficient or incompatible information is present. |
| 72 | var req request |
| 73 | |
| 74 | // Test first for unrecognized arguments. |
Ash Wilson | a87ee06 | 2014-09-03 11:26:06 -0400 | [diff] [blame] | 75 | if options.APIKey != "" { |
Ash Wilson | f8d546a | 2014-09-30 17:43:25 -0400 | [diff] [blame] | 76 | return createErr(ErrAPIKeyProvided) |
Ash Wilson | 85d8265 | 2014-08-28 13:57:46 -0400 | [diff] [blame] | 77 | } |
Ash Wilson | a87ee06 | 2014-09-03 11:26:06 -0400 | [diff] [blame] | 78 | if options.TenantID != "" { |
Ash Wilson | f8d546a | 2014-09-30 17:43:25 -0400 | [diff] [blame] | 79 | return createErr(ErrTenantIDProvided) |
Ash Wilson | 85d8265 | 2014-08-28 13:57:46 -0400 | [diff] [blame] | 80 | } |
Ash Wilson | a87ee06 | 2014-09-03 11:26:06 -0400 | [diff] [blame] | 81 | if options.TenantName != "" { |
Ash Wilson | f8d546a | 2014-09-30 17:43:25 -0400 | [diff] [blame] | 82 | return createErr(ErrTenantNameProvided) |
Ash Wilson | 85d8265 | 2014-08-28 13:57:46 -0400 | [diff] [blame] | 83 | } |
| 84 | |
Ash Wilson | a87ee06 | 2014-09-03 11:26:06 -0400 | [diff] [blame] | 85 | if options.Password == "" { |
Ash Wilson | d7f73e9 | 2014-10-22 09:11:49 -0400 | [diff] [blame] | 86 | if c.TokenID != "" { |
Ash Wilson | 85d8265 | 2014-08-28 13:57:46 -0400 | [diff] [blame] | 87 | // Because we aren't using password authentication, it's an error to also provide any of the user-based authentication |
| 88 | // parameters. |
Ash Wilson | a87ee06 | 2014-09-03 11:26:06 -0400 | [diff] [blame] | 89 | if options.Username != "" { |
Ash Wilson | f8d546a | 2014-09-30 17:43:25 -0400 | [diff] [blame] | 90 | return createErr(ErrUsernameWithToken) |
Ash Wilson | 85d8265 | 2014-08-28 13:57:46 -0400 | [diff] [blame] | 91 | } |
Ash Wilson | a87ee06 | 2014-09-03 11:26:06 -0400 | [diff] [blame] | 92 | if options.UserID != "" { |
Ash Wilson | f8d546a | 2014-09-30 17:43:25 -0400 | [diff] [blame] | 93 | return createErr(ErrUserIDWithToken) |
Ash Wilson | 85d8265 | 2014-08-28 13:57:46 -0400 | [diff] [blame] | 94 | } |
Ash Wilson | a87ee06 | 2014-09-03 11:26:06 -0400 | [diff] [blame] | 95 | if options.DomainID != "" { |
Ash Wilson | f8d546a | 2014-09-30 17:43:25 -0400 | [diff] [blame] | 96 | return createErr(ErrDomainIDWithToken) |
Ash Wilson | 85d8265 | 2014-08-28 13:57:46 -0400 | [diff] [blame] | 97 | } |
Ash Wilson | a87ee06 | 2014-09-03 11:26:06 -0400 | [diff] [blame] | 98 | if options.DomainName != "" { |
Ash Wilson | f8d546a | 2014-09-30 17:43:25 -0400 | [diff] [blame] | 99 | return createErr(ErrDomainNameWithToken) |
Ash Wilson | 85d8265 | 2014-08-28 13:57:46 -0400 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | // Configure the request for Token authentication. |
| 103 | req.Auth.Identity.Methods = []string{"token"} |
| 104 | req.Auth.Identity.Token = &tokenReq{ |
Ash Wilson | d7f73e9 | 2014-10-22 09:11:49 -0400 | [diff] [blame] | 105 | ID: c.TokenID, |
Ash Wilson | 85d8265 | 2014-08-28 13:57:46 -0400 | [diff] [blame] | 106 | } |
| 107 | } else { |
| 108 | // If no password or token ID are available, authentication can't continue. |
Ash Wilson | 55f2433 | 2014-10-02 09:37:05 -0400 | [diff] [blame] | 109 | return createErr(ErrMissingPassword) |
Ash Wilson | 85d8265 | 2014-08-28 13:57:46 -0400 | [diff] [blame] | 110 | } |
| 111 | } else { |
| 112 | // Password authentication. |
| 113 | req.Auth.Identity.Methods = []string{"password"} |
| 114 | |
| 115 | // At least one of Username and UserID must be specified. |
Ash Wilson | a87ee06 | 2014-09-03 11:26:06 -0400 | [diff] [blame] | 116 | if options.Username == "" && options.UserID == "" { |
Ash Wilson | f8d546a | 2014-09-30 17:43:25 -0400 | [diff] [blame] | 117 | return createErr(ErrUsernameOrUserID) |
Ash Wilson | 85d8265 | 2014-08-28 13:57:46 -0400 | [diff] [blame] | 118 | } |
| 119 | |
Ash Wilson | a87ee06 | 2014-09-03 11:26:06 -0400 | [diff] [blame] | 120 | if options.Username != "" { |
Ash Wilson | 85d8265 | 2014-08-28 13:57:46 -0400 | [diff] [blame] | 121 | // If Username is provided, UserID may not be provided. |
Ash Wilson | a87ee06 | 2014-09-03 11:26:06 -0400 | [diff] [blame] | 122 | if options.UserID != "" { |
Ash Wilson | f8d546a | 2014-09-30 17:43:25 -0400 | [diff] [blame] | 123 | return createErr(ErrUsernameOrUserID) |
Ash Wilson | 85d8265 | 2014-08-28 13:57:46 -0400 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | // Either DomainID or DomainName must also be specified. |
Ash Wilson | a87ee06 | 2014-09-03 11:26:06 -0400 | [diff] [blame] | 127 | if options.DomainID == "" && options.DomainName == "" { |
Ash Wilson | f8d546a | 2014-09-30 17:43:25 -0400 | [diff] [blame] | 128 | return createErr(ErrDomainIDOrDomainName) |
Ash Wilson | 85d8265 | 2014-08-28 13:57:46 -0400 | [diff] [blame] | 129 | } |
| 130 | |
Ash Wilson | a87ee06 | 2014-09-03 11:26:06 -0400 | [diff] [blame] | 131 | if options.DomainID != "" { |
| 132 | if options.DomainName != "" { |
Ash Wilson | f8d546a | 2014-09-30 17:43:25 -0400 | [diff] [blame] | 133 | return createErr(ErrDomainIDOrDomainName) |
Ash Wilson | 85d8265 | 2014-08-28 13:57:46 -0400 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | // Configure the request for Username and Password authentication with a DomainID. |
| 137 | req.Auth.Identity.Password = &passwordReq{ |
| 138 | User: userReq{ |
Ash Wilson | a87ee06 | 2014-09-03 11:26:06 -0400 | [diff] [blame] | 139 | Name: &options.Username, |
| 140 | Password: options.Password, |
| 141 | Domain: &domainReq{ID: &options.DomainID}, |
Ash Wilson | 85d8265 | 2014-08-28 13:57:46 -0400 | [diff] [blame] | 142 | }, |
| 143 | } |
| 144 | } |
| 145 | |
Ash Wilson | a87ee06 | 2014-09-03 11:26:06 -0400 | [diff] [blame] | 146 | if options.DomainName != "" { |
Ash Wilson | 85d8265 | 2014-08-28 13:57:46 -0400 | [diff] [blame] | 147 | // Configure the request for Username and Password authentication with a DomainName. |
| 148 | req.Auth.Identity.Password = &passwordReq{ |
| 149 | User: userReq{ |
Ash Wilson | a87ee06 | 2014-09-03 11:26:06 -0400 | [diff] [blame] | 150 | Name: &options.Username, |
| 151 | Password: options.Password, |
| 152 | Domain: &domainReq{Name: &options.DomainName}, |
Ash Wilson | 85d8265 | 2014-08-28 13:57:46 -0400 | [diff] [blame] | 153 | }, |
| 154 | } |
| 155 | } |
| 156 | } |
| 157 | |
Ash Wilson | a87ee06 | 2014-09-03 11:26:06 -0400 | [diff] [blame] | 158 | if options.UserID != "" { |
Ash Wilson | 85d8265 | 2014-08-28 13:57:46 -0400 | [diff] [blame] | 159 | // If UserID is specified, neither DomainID nor DomainName may be. |
Ash Wilson | a87ee06 | 2014-09-03 11:26:06 -0400 | [diff] [blame] | 160 | if options.DomainID != "" { |
Ash Wilson | f8d546a | 2014-09-30 17:43:25 -0400 | [diff] [blame] | 161 | return createErr(ErrDomainIDWithUserID) |
Ash Wilson | 85d8265 | 2014-08-28 13:57:46 -0400 | [diff] [blame] | 162 | } |
Ash Wilson | a87ee06 | 2014-09-03 11:26:06 -0400 | [diff] [blame] | 163 | if options.DomainName != "" { |
Ash Wilson | f8d546a | 2014-09-30 17:43:25 -0400 | [diff] [blame] | 164 | return createErr(ErrDomainNameWithUserID) |
Ash Wilson | 85d8265 | 2014-08-28 13:57:46 -0400 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | // Configure the request for UserID and Password authentication. |
| 168 | req.Auth.Identity.Password = &passwordReq{ |
Ash Wilson | a87ee06 | 2014-09-03 11:26:06 -0400 | [diff] [blame] | 169 | User: userReq{ID: &options.UserID, Password: options.Password}, |
Ash Wilson | 85d8265 | 2014-08-28 13:57:46 -0400 | [diff] [blame] | 170 | } |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | // Add a "scope" element if a Scope has been provided. |
| 175 | if scope != nil { |
| 176 | if scope.ProjectName != "" { |
| 177 | // ProjectName provided: either DomainID or DomainName must also be supplied. |
| 178 | // ProjectID may not be supplied. |
| 179 | if scope.DomainID == "" && scope.DomainName == "" { |
Ash Wilson | f8d546a | 2014-09-30 17:43:25 -0400 | [diff] [blame] | 180 | return createErr(ErrScopeDomainIDOrDomainName) |
Ash Wilson | 85d8265 | 2014-08-28 13:57:46 -0400 | [diff] [blame] | 181 | } |
| 182 | if scope.ProjectID != "" { |
Ash Wilson | f8d546a | 2014-09-30 17:43:25 -0400 | [diff] [blame] | 183 | return createErr(ErrScopeProjectIDOrProjectName) |
Ash Wilson | 85d8265 | 2014-08-28 13:57:46 -0400 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | if scope.DomainID != "" { |
| 187 | // ProjectName + DomainID |
| 188 | req.Auth.Scope = &scopeReq{ |
| 189 | Project: &projectReq{ |
| 190 | Name: &scope.ProjectName, |
| 191 | Domain: &domainReq{ID: &scope.DomainID}, |
| 192 | }, |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | if scope.DomainName != "" { |
| 197 | // ProjectName + DomainName |
| 198 | req.Auth.Scope = &scopeReq{ |
| 199 | Project: &projectReq{ |
| 200 | Name: &scope.ProjectName, |
| 201 | Domain: &domainReq{Name: &scope.DomainName}, |
| 202 | }, |
| 203 | } |
| 204 | } |
| 205 | } else if scope.ProjectID != "" { |
| 206 | // ProjectID provided. ProjectName, DomainID, and DomainName may not be provided. |
Ash Wilson | 85d8265 | 2014-08-28 13:57:46 -0400 | [diff] [blame] | 207 | if scope.DomainID != "" { |
Ash Wilson | f8d546a | 2014-09-30 17:43:25 -0400 | [diff] [blame] | 208 | return createErr(ErrScopeProjectIDAlone) |
Ash Wilson | 85d8265 | 2014-08-28 13:57:46 -0400 | [diff] [blame] | 209 | } |
| 210 | if scope.DomainName != "" { |
Ash Wilson | f8d546a | 2014-09-30 17:43:25 -0400 | [diff] [blame] | 211 | return createErr(ErrScopeProjectIDAlone) |
Ash Wilson | 85d8265 | 2014-08-28 13:57:46 -0400 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | // ProjectID |
| 215 | req.Auth.Scope = &scopeReq{ |
| 216 | Project: &projectReq{ID: &scope.ProjectID}, |
| 217 | } |
| 218 | } else if scope.DomainID != "" { |
| 219 | // DomainID provided. ProjectID, ProjectName, and DomainName may not be provided. |
| 220 | if scope.DomainName != "" { |
Ash Wilson | f8d546a | 2014-09-30 17:43:25 -0400 | [diff] [blame] | 221 | return createErr(ErrScopeDomainIDOrDomainName) |
Ash Wilson | 85d8265 | 2014-08-28 13:57:46 -0400 | [diff] [blame] | 222 | } |
| 223 | |
| 224 | // DomainID |
| 225 | req.Auth.Scope = &scopeReq{ |
| 226 | Domain: &domainReq{ID: &scope.DomainID}, |
| 227 | } |
| 228 | } else if scope.DomainName != "" { |
Ash Wilson | f8d546a | 2014-09-30 17:43:25 -0400 | [diff] [blame] | 229 | return createErr(ErrScopeDomainName) |
Ash Wilson | 85d8265 | 2014-08-28 13:57:46 -0400 | [diff] [blame] | 230 | } else { |
Ash Wilson | f8d546a | 2014-09-30 17:43:25 -0400 | [diff] [blame] | 231 | return createErr(ErrScopeEmpty) |
Ash Wilson | 85d8265 | 2014-08-28 13:57:46 -0400 | [diff] [blame] | 232 | } |
| 233 | } |
| 234 | |
Ash Wilson | f8d546a | 2014-09-30 17:43:25 -0400 | [diff] [blame] | 235 | var result CreateResult |
| 236 | var response *perigee.Response |
| 237 | response, result.Err = perigee.Request("POST", tokenURL(c), perigee.Options{ |
Ash Wilson | 85d8265 | 2014-08-28 13:57:46 -0400 | [diff] [blame] | 238 | ReqBody: &req, |
Ash Wilson | d3dc254 | 2014-10-20 10:10:48 -0400 | [diff] [blame] | 239 | Results: &result.Body, |
Ash Wilson | 85d8265 | 2014-08-28 13:57:46 -0400 | [diff] [blame] | 240 | OkCodes: []int{201}, |
| 241 | }) |
Ash Wilson | f8d546a | 2014-09-30 17:43:25 -0400 | [diff] [blame] | 242 | if result.Err != nil { |
| 243 | return result |
Ash Wilson | 4a52e2a | 2014-08-29 09:28:00 -0400 | [diff] [blame] | 244 | } |
Ash Wilson | 72e4d2c | 2014-10-20 10:27:30 -0400 | [diff] [blame] | 245 | result.Header = response.HttpResponse.Header |
Ash Wilson | f8d546a | 2014-09-30 17:43:25 -0400 | [diff] [blame] | 246 | return result |
Ash Wilson | 85d8265 | 2014-08-28 13:57:46 -0400 | [diff] [blame] | 247 | } |
Ash Wilson | 46d913f | 2014-08-29 11:00:11 -0400 | [diff] [blame] | 248 | |
Ash Wilson | 5266e49 | 2014-09-09 15:44:30 -0400 | [diff] [blame] | 249 | // Get validates and retrieves information about another token. |
Ash Wilson | f8d546a | 2014-09-30 17:43:25 -0400 | [diff] [blame] | 250 | func Get(c *gophercloud.ServiceClient, token string) GetResult { |
| 251 | var result GetResult |
| 252 | var response *perigee.Response |
| 253 | response, result.Err = perigee.Request("GET", tokenURL(c), perigee.Options{ |
Ash Wilson | 46d913f | 2014-08-29 11:00:11 -0400 | [diff] [blame] | 254 | MoreHeaders: subjectTokenHeaders(c, token), |
Ash Wilson | d3dc254 | 2014-10-20 10:10:48 -0400 | [diff] [blame] | 255 | Results: &result.Body, |
Ash Wilson | 46d913f | 2014-08-29 11:00:11 -0400 | [diff] [blame] | 256 | OkCodes: []int{200, 203}, |
| 257 | }) |
Ash Wilson | f8d546a | 2014-09-30 17:43:25 -0400 | [diff] [blame] | 258 | if result.Err != nil { |
| 259 | return result |
Ash Wilson | 46d913f | 2014-08-29 11:00:11 -0400 | [diff] [blame] | 260 | } |
Ash Wilson | 72e4d2c | 2014-10-20 10:27:30 -0400 | [diff] [blame] | 261 | result.Header = response.HttpResponse.Header |
Ash Wilson | f8d546a | 2014-09-30 17:43:25 -0400 | [diff] [blame] | 262 | return result |
Ash Wilson | 46d913f | 2014-08-29 11:00:11 -0400 | [diff] [blame] | 263 | } |
| 264 | |
| 265 | // Validate determines if a specified token is valid or not. |
Ash Wilson | 6425a41 | 2014-08-29 12:30:35 -0400 | [diff] [blame] | 266 | func Validate(c *gophercloud.ServiceClient, token string) (bool, error) { |
Ash Wilson | 1e1b733 | 2014-09-30 16:38:11 -0400 | [diff] [blame] | 267 | response, err := perigee.Request("HEAD", tokenURL(c), perigee.Options{ |
Ash Wilson | 46d913f | 2014-08-29 11:00:11 -0400 | [diff] [blame] | 268 | MoreHeaders: subjectTokenHeaders(c, token), |
| 269 | OkCodes: []int{204, 404}, |
| 270 | }) |
| 271 | if err != nil { |
| 272 | return false, err |
| 273 | } |
| 274 | |
| 275 | return response.StatusCode == 204, nil |
| 276 | } |
| 277 | |
| 278 | // Revoke immediately makes specified token invalid. |
Ash Wilson | 6425a41 | 2014-08-29 12:30:35 -0400 | [diff] [blame] | 279 | func Revoke(c *gophercloud.ServiceClient, token string) error { |
Ash Wilson | 1e1b733 | 2014-09-30 16:38:11 -0400 | [diff] [blame] | 280 | _, err := perigee.Request("DELETE", tokenURL(c), perigee.Options{ |
Ash Wilson | 46d913f | 2014-08-29 11:00:11 -0400 | [diff] [blame] | 281 | MoreHeaders: subjectTokenHeaders(c, token), |
| 282 | OkCodes: []int{204}, |
| 283 | }) |
| 284 | return err |
| 285 | } |