blob: 69781864bd7fce22b1d2f768f5c24ad424f0b6ef [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 Perritt2be387a2016-03-31 09:31:58 -050023func Create(c *gophercloud.ServiceClient, opts AuthOptionsBuilder, scopeOpts *gophercloud.ScopeOptsV3) (r CreateResult) {
Jon Perrittdb0ae142016-03-13 00:33:41 -060024 b, err := opts.ToTokenV3CreateMap(scopeOpts)
25 if err != nil {
26 r.Err = err
Jon Perritt2be387a2016-03-31 09:31:58 -050027 return
Ash Wilson85d82652014-08-28 13:57:46 -040028 }
Jon Perrittdb0ae142016-03-13 00:33:41 -060029 var resp *http.Response
30 resp, r.Err = c.Post(tokenURL(c), b, &r.Body, nil)
31 if resp != nil {
32 r.Header = resp.Header
Ash Wilson85d82652014-08-28 13:57:46 -040033 }
Ash Wilson85d82652014-08-28 13:57:46 -040034}
Ash Wilson46d913f2014-08-29 11:00:11 -040035
Ash Wilson5266e492014-09-09 15:44:30 -040036// Get validates and retrieves information about another token.
Jon Perritt2be387a2016-03-31 09:31:58 -050037func Get(c *gophercloud.ServiceClient, token string) (r GetResult) {
Jon Perrittdb0ae142016-03-13 00:33:41 -060038 var resp *http.Response
39 resp, r.Err = c.Get(tokenURL(c), &r.Body, &gophercloud.RequestOpts{
Jamie Hannaford562a7d52015-03-24 16:20:16 +010040 MoreHeaders: subjectTokenHeaders(c, token),
41 OkCodes: []int{200, 203},
Ash Wilson46d913f2014-08-29 11:00:11 -040042 })
Jon Perrittdb0ae142016-03-13 00:33:41 -060043 if resp != nil {
44 r.Header = resp.Header
Ash Wilson46d913f2014-08-29 11:00:11 -040045 }
Ash Wilson46d913f2014-08-29 11:00:11 -040046}
47
48// Validate determines if a specified token is valid or not.
Ash Wilson6425a412014-08-29 12:30:35 -040049func Validate(c *gophercloud.ServiceClient, token string) (bool, error) {
Jon Perritta33da232016-03-02 04:43:08 -060050 response, err := c.Request("HEAD", tokenURL(c), &gophercloud.RequestOpts{
Ash Wilson46d913f2014-08-29 11:00:11 -040051 MoreHeaders: subjectTokenHeaders(c, token),
52 OkCodes: []int{204, 404},
53 })
54 if err != nil {
55 return false, err
56 }
57
58 return response.StatusCode == 204, nil
59}
60
61// Revoke immediately makes specified token invalid.
Jon Perritt2be387a2016-03-31 09:31:58 -050062func Revoke(c *gophercloud.ServiceClient, token string) (r RevokeResult) {
Jon Perrittdb0ae142016-03-13 00:33:41 -060063 _, r.Err = c.Delete(tokenURL(c), &gophercloud.RequestOpts{
Ash Wilson46d913f2014-08-29 11:00:11 -040064 MoreHeaders: subjectTokenHeaders(c, token),
Ash Wilson46d913f2014-08-29 11:00:11 -040065 })
Ash Wilson46d913f2014-08-29 11:00:11 -040066}