blob: e7320fd942f1b5e28f610c2dd0577de7d9e95824 [file] [log] [blame]
Ash Wilson85d82652014-08-28 13:57:46 -04001package tokens
2
3import (
Ash Wilson2491b4c2015-02-12 16:13:39 -05004 "net/http"
5
Jon Perritt27249f42016-02-18 10:35:59 -06006 "github.com/gophercloud/gophercloud"
Ash Wilson85d82652014-08-28 13:57:46 -04007)
8
Jon Perrittdb0ae142016-03-13 00:33:41 -06009// AuthOptionsBuilder describes any argument that may be passed to the Create call.
10type 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 Wilson85d82652014-08-28 13:57:46 -040014}
15
Ash Wilson6425a412014-08-29 12:30:35 -040016func subjectTokenHeaders(c *gophercloud.ServiceClient, subjectToken string) map[string]string {
Krzysztof Kwapisiewiczbaaaf3e2016-02-03 15:18:16 +010017 return map[string]string{
18 "X-Subject-Token": subjectToken,
19 }
Ash Wilson46d913f2014-08-29 11:00:11 -040020}
21
Ash Wilsone5550862014-08-28 15:37:09 -040022// Create authenticates and either generates a new token, or changes the Scope of an existing token.
Jon Perrittdb0ae142016-03-13 00:33:41 -060023func 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 Wilson85d82652014-08-28 13:57:46 -040029 }
Jon Perrittdb0ae142016-03-13 00:33:41 -060030 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 Wilson85d82652014-08-28 13:57:46 -040034 }
Jon Perrittdb0ae142016-03-13 00:33:41 -060035 return r
Ash Wilson85d82652014-08-28 13:57:46 -040036}
Ash Wilson46d913f2014-08-29 11:00:11 -040037
Ash Wilson5266e492014-09-09 15:44:30 -040038// Get validates and retrieves information about another token.
Ash Wilsonf8d546a2014-09-30 17:43:25 -040039func Get(c *gophercloud.ServiceClient, token string) GetResult {
Jon Perrittdb0ae142016-03-13 00:33:41 -060040 var r GetResult
41 var resp *http.Response
42 resp, r.Err = c.Get(tokenURL(c), &r.Body, &gophercloud.RequestOpts{
Jamie Hannaford562a7d52015-03-24 16:20:16 +010043 MoreHeaders: subjectTokenHeaders(c, token),
44 OkCodes: []int{200, 203},
Ash Wilson46d913f2014-08-29 11:00:11 -040045 })
Jon Perrittdb0ae142016-03-13 00:33:41 -060046 if resp != nil {
47 r.Header = resp.Header
Ash Wilson46d913f2014-08-29 11:00:11 -040048 }
Jon Perrittdb0ae142016-03-13 00:33:41 -060049 return r
Ash Wilson46d913f2014-08-29 11:00:11 -040050}
51
52// Validate determines if a specified token is valid or not.
Ash Wilson6425a412014-08-29 12:30:35 -040053func Validate(c *gophercloud.ServiceClient, token string) (bool, error) {
Jon Perritta33da232016-03-02 04:43:08 -060054 response, err := c.Request("HEAD", tokenURL(c), &gophercloud.RequestOpts{
Ash Wilson46d913f2014-08-29 11:00:11 -040055 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 Hannafordf38dd2e2014-10-27 11:36:54 +010066func Revoke(c *gophercloud.ServiceClient, token string) RevokeResult {
Jon Perrittdb0ae142016-03-13 00:33:41 -060067 var r RevokeResult
68 _, r.Err = c.Delete(tokenURL(c), &gophercloud.RequestOpts{
Ash Wilson46d913f2014-08-29 11:00:11 -040069 MoreHeaders: subjectTokenHeaders(c, token),
Ash Wilson46d913f2014-08-29 11:00:11 -040070 })
Jon Perrittdb0ae142016-03-13 00:33:41 -060071 return r
Ash Wilson46d913f2014-08-29 11:00:11 -040072}