blob: df67d765b18e0fad2b697273c718cf9d48a97c9c [file] [log] [blame]
Ash Wilson31844f22014-09-08 15:32:58 -04001package v2
Samuel A. Falvo II8a549ef2014-01-24 15:20:54 -08002
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 {
Ash Wilson12b3b6c2014-09-08 14:54:38 -040024 ID, Expires string
25 TenantID, TenantName string
Samuel A. Falvo II8a549ef2014-01-24 15:20:54 -080026}
27
Ash Wilson12b3b6c2014-09-08 14:54:38 -040028// GetToken yields an unpacked collection of fields related to the user's access credentials, called a "token", if successful.
Samuel A. Falvo II2b963212014-02-09 02:12:30 -080029// See the Token structure for more details.
30func GetToken(m AuthResults) (*Token, error) {
31 type (
32 Tenant struct {
Ash Wilson12b3b6c2014-09-08 14:54:38 -040033 ID string
Samuel A. Falvo II2b963212014-02-09 02:12:30 -080034 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 {
Ash Wilson12b3b6c2014-09-08 14:54:38 -040038 ID string `mapstructure:"id"`
Samuel A. Falvo II2b963212014-02-09 02:12:30 -080039 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{
Ash Wilson12b3b6c2014-09-08 14:54:38 -040058 ID: t.ID,
Samuel A. Falvo II2b963212014-02-09 02:12:30 -080059 Expires: t.Expires,
Ash Wilson12b3b6c2014-09-08 14:54:38 -040060 TenantID: t.Tenant.ID,
Samuel A. Falvo II2b963212014-02-09 02:12:30 -080061 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}