token.go (1691B)
1 package osiam 2 3 import ( 4 "fmt" 5 "strconv" 6 "time" 7 ) 8 9 // Token represents an osiam auth token 10 type Token struct { 11 TokenType string `json:"token_type"` // example "bearer" 12 AccessToken string `json:"access_token"` // example "79f479c2-c0d7-458a-8464-7eb887dbc943" 13 RefreshToken string `json:"refresh_token"` // example "3c7c4a87-dc91-4dd0-8ec8-d229a237a47c" 14 ClientID string `json:"client_id"` // example "example-client" 15 UserName string `json:"user_name"` // example "admin" 16 Userid string `json:"user_id"` // example "84f6cffa-4505-48ec-a851-424160892283" 17 Scope string `json:"scope"` // example "ME" 18 RefreshTokenExpiresAt Timestamp `json:"refresh_token_expires_at"` // example 1479309001813 19 ExpiresAt Timestamp `json:"expires_at"` // example 1479251401814 20 ExpiresIn int `json:"expires_in"` // example 28795 21 } 22 23 // Timestamp is a helper class for json handling ht the timestamp 24 type Timestamp struct { 25 T time.Time 26 } 27 28 // UnmarshalJSON does the unmarshaling 29 func (timestamp *Timestamp) UnmarshalJSON(b []byte) (err error) { 30 i, err := strconv.ParseInt(string(b), 10, 64) 31 if err != nil { 32 return err 33 } 34 timestamp.T = time.Unix(i, 0) 35 return nil 36 } 37 38 // MarshalJSON does the marshaling 39 func (timestamp *Timestamp) MarshalJSON() ([]byte, error) { 40 if timestamp.T.UnixNano() == nilTime { 41 return []byte("null"), nil 42 } 43 return []byte(fmt.Sprintf("%d", timestamp.T.Unix())), nil 44 } 45 46 var nilTime = (time.Time{}).UnixNano()