blob: d50cce0446a4e52fe12e5b5fec069639a56aee69 [file] [log] [blame]
Samuel A. Falvo II8a549ef2014-01-24 15:20:54 -08001package identity
2
3import (
4 "github.com/mitchellh/mapstructure"
5)
6
Samuel A. Falvo II2b963212014-02-09 02:12:30 -08007// Token provides only the most basic information related to an authentication token.
8//
9// Id provides the primary means of identifying a user to the OpenStack API.
10// OpenStack defines this field as an opaque value, so do not depend on its content.
11// It is safe, however, to compare for equality.
12//
13// Expires provides a timestamp in ISO 8601 format, indicating when the authentication token becomes invalid.
14// After this point in time, future API requests made using this authentication token will respond with errors.
15// Either the caller will need to reauthenticate manually, or more preferably, the caller should exploit automatic re-authentication.
16// See the AuthOptions structure for more details.
17//
18// TenantId provides the canonical means of identifying a tenant.
19// As with Id, this field is defined to be opaque, so do not depend on its content.
20// It is safe, however, to compare for equality.
21//
22// TenantName provides a human-readable tenant name corresponding to the TenantId.
23type Token struct {
24 Id, Expires string
25 TenantId, TenantName string
Samuel A. Falvo II8a549ef2014-01-24 15:20:54 -080026}
27
Samuel A. Falvo II2b963212014-02-09 02:12:30 -080028// GetToken, if successful, yields an unpacked collection of fields related to the user's access credentials, called a "token."
29// See the Token structure for more details.
30func GetToken(m AuthResults) (*Token, error) {
31 type (
32 Tenant struct {
33 Id string
34 Name string
35 }
Samuel A. Falvo II8a549ef2014-01-24 15:20:54 -080036
Samuel A. Falvo II2b963212014-02-09 02:12:30 -080037 TokenDesc struct {
38 Id string `mapstructure:"id"`
39 Expires string `mapstructure:"expires"`
40 Tenant
41 }
42 )
43
44 accessMap, err := getSubmap(m, "access")
Samuel A. Falvo II8a549ef2014-01-24 15:20:54 -080045 if err != nil {
46 return nil, err
47 }
Samuel A. Falvo II2b963212014-02-09 02:12:30 -080048 tokenMap, err := getSubmap(accessMap, "token")
49 if err != nil {
50 return nil, err
51 }
52 t := &TokenDesc{}
53 err = mapstructure.Decode(tokenMap, t)
54 if err != nil {
55 return nil, err
56 }
57 td := &Token{
58 Id: t.Id,
59 Expires: t.Expires,
60 TenantId: t.Tenant.Id,
61 TenantName: t.Tenant.Name,
62 }
Samuel A. Falvo II8a549ef2014-01-24 15:20:54 -080063 return td, nil
64}
65
Samuel A. Falvo II2b963212014-02-09 02:12:30 -080066func getSubmap(m map[string]interface{}, name string) (map[string]interface{}, error) {
67 entry, ok := m[name]
68 if !ok {
69 return nil, ErrNotImplemented
70 }
71 return entry.(map[string]interface{}), nil
Samuel A. Falvo II8a549ef2014-01-24 15:20:54 -080072}