Ash Wilson | 85d8265 | 2014-08-28 13:57:46 -0400 | [diff] [blame] | 1 | package tokens |
| 2 | |
| 3 | import ( |
Ash Wilson | 2491b4c | 2015-02-12 16:13:39 -0500 | [diff] [blame] | 4 | "net/http" |
| 5 | |
Jon Perritt | 27249f4 | 2016-02-18 10:35:59 -0600 | [diff] [blame] | 6 | "github.com/gophercloud/gophercloud" |
Ash Wilson | 85d8265 | 2014-08-28 13:57:46 -0400 | [diff] [blame] | 7 | ) |
| 8 | |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 9 | // AuthOptionsBuilder describes any argument that may be passed to the Create call. |
| 10 | type AuthOptionsBuilder interface { |
| 11 | // ToTokenV3CreateMap assembles the Create request body, returning an error if parameters are |
| 12 | // missing or inconsistent. |
| 13 | ToTokenV3CreateMap(*gophercloud.ScopeOptsV3) (map[string]interface{}, error) |
Ash Wilson | 85d8265 | 2014-08-28 13:57:46 -0400 | [diff] [blame] | 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 { |
Krzysztof Kwapisiewicz | baaaf3e | 2016-02-03 15:18:16 +0100 | [diff] [blame] | 17 | return map[string]string{ |
| 18 | "X-Subject-Token": subjectToken, |
| 19 | } |
Ash Wilson | 46d913f | 2014-08-29 11:00:11 -0400 | [diff] [blame] | 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. |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 23 | func Create(c *gophercloud.ServiceClient, opts AuthOptionsBuilder, scopeOpts *gophercloud.ScopeOptsV3) CreateResult { |
| 24 | var r CreateResult |
| 25 | b, err := opts.ToTokenV3CreateMap(scopeOpts) |
| 26 | if err != nil { |
| 27 | r.Err = err |
| 28 | return r |
Ash Wilson | 85d8265 | 2014-08-28 13:57:46 -0400 | [diff] [blame] | 29 | } |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 30 | var resp *http.Response |
| 31 | resp, r.Err = c.Post(tokenURL(c), b, &r.Body, nil) |
| 32 | if resp != nil { |
| 33 | r.Header = resp.Header |
Ash Wilson | 85d8265 | 2014-08-28 13:57:46 -0400 | [diff] [blame] | 34 | } |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 35 | return r |
Ash Wilson | 85d8265 | 2014-08-28 13:57:46 -0400 | [diff] [blame] | 36 | } |
Ash Wilson | 46d913f | 2014-08-29 11:00:11 -0400 | [diff] [blame] | 37 | |
Ash Wilson | 5266e49 | 2014-09-09 15:44:30 -0400 | [diff] [blame] | 38 | // Get validates and retrieves information about another token. |
Ash Wilson | f8d546a | 2014-09-30 17:43:25 -0400 | [diff] [blame] | 39 | func Get(c *gophercloud.ServiceClient, token string) GetResult { |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 40 | var r GetResult |
| 41 | var resp *http.Response |
| 42 | resp, r.Err = c.Get(tokenURL(c), &r.Body, &gophercloud.RequestOpts{ |
Jamie Hannaford | 562a7d5 | 2015-03-24 16:20:16 +0100 | [diff] [blame] | 43 | MoreHeaders: subjectTokenHeaders(c, token), |
| 44 | OkCodes: []int{200, 203}, |
Ash Wilson | 46d913f | 2014-08-29 11:00:11 -0400 | [diff] [blame] | 45 | }) |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 46 | if resp != nil { |
| 47 | r.Header = resp.Header |
Ash Wilson | 46d913f | 2014-08-29 11:00:11 -0400 | [diff] [blame] | 48 | } |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 49 | return r |
Ash Wilson | 46d913f | 2014-08-29 11:00:11 -0400 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | // Validate determines if a specified token is valid or not. |
Ash Wilson | 6425a41 | 2014-08-29 12:30:35 -0400 | [diff] [blame] | 53 | func Validate(c *gophercloud.ServiceClient, token string) (bool, error) { |
Jon Perritt | a33da23 | 2016-03-02 04:43:08 -0600 | [diff] [blame] | 54 | response, err := c.Request("HEAD", tokenURL(c), &gophercloud.RequestOpts{ |
Ash Wilson | 46d913f | 2014-08-29 11:00:11 -0400 | [diff] [blame] | 55 | MoreHeaders: subjectTokenHeaders(c, token), |
| 56 | OkCodes: []int{204, 404}, |
| 57 | }) |
| 58 | if err != nil { |
| 59 | return false, err |
| 60 | } |
| 61 | |
| 62 | return response.StatusCode == 204, nil |
| 63 | } |
| 64 | |
| 65 | // Revoke immediately makes specified token invalid. |
Jamie Hannaford | f38dd2e | 2014-10-27 11:36:54 +0100 | [diff] [blame] | 66 | func Revoke(c *gophercloud.ServiceClient, token string) RevokeResult { |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 67 | var r RevokeResult |
| 68 | _, r.Err = c.Delete(tokenURL(c), &gophercloud.RequestOpts{ |
Ash Wilson | 46d913f | 2014-08-29 11:00:11 -0400 | [diff] [blame] | 69 | MoreHeaders: subjectTokenHeaders(c, token), |
Ash Wilson | 46d913f | 2014-08-29 11:00:11 -0400 | [diff] [blame] | 70 | }) |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 71 | return r |
Ash Wilson | 46d913f | 2014-08-29 11:00:11 -0400 | [diff] [blame] | 72 | } |