github.com/mook-as/cf-cli@v7.0.0-beta.28.0.20200120190804-b91c115fae48+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 ClientID string `json:"client_id"` 12 Email string `json:"email"` 13 UserGUID string `json:"user_id"` 14 } 15 16 func NewTokenInfo(accessToken string) (info TokenInfo) { 17 tokenJSON, err := DecodeAccessToken(accessToken) 18 if err != nil { 19 return TokenInfo{} 20 } 21 22 info = TokenInfo{} 23 err = json.Unmarshal(tokenJSON, &info) 24 if err != nil { 25 return TokenInfo{} 26 } 27 28 return info 29 } 30 31 func DecodeAccessToken(accessToken string) (tokenJSON []byte, err error) { 32 tokenParts := strings.Split(accessToken, " ") 33 34 if len(tokenParts) < 2 { 35 return 36 } 37 38 token := tokenParts[1] 39 encodedParts := strings.Split(token, ".") 40 41 if len(encodedParts) < 3 { 42 return 43 } 44 45 encodedTokenJSON := encodedParts[1] 46 return base64Decode(encodedTokenJSON) 47 } 48 49 func base64Decode(encodedData string) ([]byte, error) { 50 return base64.StdEncoding.DecodeString(restorePadding(encodedData)) 51 } 52 53 func restorePadding(seg string) string { 54 switch len(seg) % 4 { 55 case 2: 56 seg = seg + "==" 57 case 3: 58 seg = seg + "=" 59 } 60 return seg 61 }