github.com/loggregator/cli@v6.33.1-0.20180224010324-82334f081791+incompatible/cf/configuration/coreconfig/access_token.go (about) 1 package coreconfig 2 3 import ( 4 "encoding/base64" 5 "encoding/json" 6 "strings" 7 ) 8 9 type TokenInfo struct { 10 Username string `json:"user_name"` 11 Email string `json:"email"` 12 UserGUID string `json:"user_id"` 13 } 14 15 func NewTokenInfo(accessToken string) (info TokenInfo) { 16 tokenJSON, err := DecodeAccessToken(accessToken) 17 if err != nil { 18 return TokenInfo{} 19 } 20 21 info = TokenInfo{} 22 err = json.Unmarshal(tokenJSON, &info) 23 if err != nil { 24 return TokenInfo{} 25 } 26 27 return info 28 } 29 30 func DecodeAccessToken(accessToken string) (tokenJSON []byte, err error) { 31 tokenParts := strings.Split(accessToken, " ") 32 33 if len(tokenParts) < 2 { 34 return 35 } 36 37 token := tokenParts[1] 38 encodedParts := strings.Split(token, ".") 39 40 if len(encodedParts) < 3 { 41 return 42 } 43 44 encodedTokenJSON := encodedParts[1] 45 return base64Decode(encodedTokenJSON) 46 } 47 48 func base64Decode(encodedData string) ([]byte, error) { 49 return base64.StdEncoding.DecodeString(restorePadding(encodedData)) 50 } 51 52 func restorePadding(seg string) string { 53 switch len(seg) % 4 { 54 case 2: 55 seg = seg + "==" 56 case 3: 57 seg = seg + "=" 58 } 59 return seg 60 }