Ash Wilson | 31844f2 | 2014-09-08 15:32:58 -0400 | [diff] [blame] | 1 | package v2 |
Samuel A. Falvo II | 8a549ef | 2014-01-24 15:20:54 -0800 | [diff] [blame] | 2 | |
| 3 | import ( |
| 4 | "github.com/mitchellh/mapstructure" |
| 5 | ) |
| 6 | |
Samuel A. Falvo II | 2b96321 | 2014-02-09 02:12:30 -0800 | [diff] [blame] | 7 | // 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. |
| 23 | type Token struct { |
Ash Wilson | 12b3b6c | 2014-09-08 14:54:38 -0400 | [diff] [blame] | 24 | ID, Expires string |
| 25 | TenantID, TenantName string |
Samuel A. Falvo II | 8a549ef | 2014-01-24 15:20:54 -0800 | [diff] [blame] | 26 | } |
| 27 | |
Ash Wilson | 12b3b6c | 2014-09-08 14:54:38 -0400 | [diff] [blame] | 28 | // GetToken yields an unpacked collection of fields related to the user's access credentials, called a "token", if successful. |
Samuel A. Falvo II | 2b96321 | 2014-02-09 02:12:30 -0800 | [diff] [blame] | 29 | // See the Token structure for more details. |
| 30 | func GetToken(m AuthResults) (*Token, error) { |
| 31 | type ( |
| 32 | Tenant struct { |
Ash Wilson | 12b3b6c | 2014-09-08 14:54:38 -0400 | [diff] [blame] | 33 | ID string |
Samuel A. Falvo II | 2b96321 | 2014-02-09 02:12:30 -0800 | [diff] [blame] | 34 | Name string |
| 35 | } |
Samuel A. Falvo II | 8a549ef | 2014-01-24 15:20:54 -0800 | [diff] [blame] | 36 | |
Samuel A. Falvo II | 2b96321 | 2014-02-09 02:12:30 -0800 | [diff] [blame] | 37 | TokenDesc struct { |
Ash Wilson | 12b3b6c | 2014-09-08 14:54:38 -0400 | [diff] [blame] | 38 | ID string `mapstructure:"id"` |
Samuel A. Falvo II | 2b96321 | 2014-02-09 02:12:30 -0800 | [diff] [blame] | 39 | Expires string `mapstructure:"expires"` |
| 40 | Tenant |
| 41 | } |
| 42 | ) |
| 43 | |
| 44 | accessMap, err := getSubmap(m, "access") |
Samuel A. Falvo II | 8a549ef | 2014-01-24 15:20:54 -0800 | [diff] [blame] | 45 | if err != nil { |
| 46 | return nil, err |
| 47 | } |
Samuel A. Falvo II | 2b96321 | 2014-02-09 02:12:30 -0800 | [diff] [blame] | 48 | 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 Wilson | 12b3b6c | 2014-09-08 14:54:38 -0400 | [diff] [blame] | 58 | ID: t.ID, |
Samuel A. Falvo II | 2b96321 | 2014-02-09 02:12:30 -0800 | [diff] [blame] | 59 | Expires: t.Expires, |
Ash Wilson | 12b3b6c | 2014-09-08 14:54:38 -0400 | [diff] [blame] | 60 | TenantID: t.Tenant.ID, |
Samuel A. Falvo II | 2b96321 | 2014-02-09 02:12:30 -0800 | [diff] [blame] | 61 | TenantName: t.Tenant.Name, |
| 62 | } |
Samuel A. Falvo II | 8a549ef | 2014-01-24 15:20:54 -0800 | [diff] [blame] | 63 | return td, nil |
| 64 | } |
| 65 | |
Samuel A. Falvo II | 2b96321 | 2014-02-09 02:12:30 -0800 | [diff] [blame] | 66 | func 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 II | 8a549ef | 2014-01-24 15:20:54 -0800 | [diff] [blame] | 72 | } |