github.com/aporeto-inc/trireme-lib@v10.358.0+incompatible/controller/pkg/usertokens/common/common.go (about) 1 package common 2 3 import ( 4 "reflect" 5 "strconv" 6 ) 7 8 // JWTType is the type of user JWTs that must be implemented. 9 type JWTType int 10 11 // Values of JWTType 12 const ( 13 PKI JWTType = iota 14 OIDC 15 ) 16 17 // convert an unknown int type to int64 18 func toInt64(i interface{}) int64 { 19 switch i := i.(type) { 20 case int: 21 return int64(i) 22 case int8: 23 return int64(i) 24 case int16: 25 return int64(i) 26 case int32: 27 return int64(i) 28 case int64: 29 return i 30 default: 31 panic("toInt64(): expected a signed integer type, got " + reflect.TypeOf(i).String() + " instead") 32 } 33 } 34 35 // convert an unknown int type to int64 36 func toUint64(i interface{}) uint64 { 37 switch i := i.(type) { 38 case uint: 39 return uint64(i) 40 case uint8: 41 return uint64(i) 42 case uint16: 43 return uint64(i) 44 case uint32: 45 return uint64(i) 46 case uint64: 47 return i 48 default: 49 panic("toUint64(): expected an unsigned integer type, got " + reflect.TypeOf(i).String() + " instead") 50 } 51 } 52 53 // FlattenClaim flattens all the generic claims in a flat array for strings. 54 func FlattenClaim(key string, claim interface{}) []string { 55 attributes := []string{} 56 57 switch claim := claim.(type) { 58 case bool: 59 attributes = append(attributes, key+"="+strconv.FormatBool(claim)) 60 case int, int8, int16, int32, int64: 61 attributes = append(attributes, key+"="+strconv.FormatInt(toInt64(claim), 10)) 62 case uint, uint8, uint16, uint32, uint64: 63 attributes = append(attributes, key+"="+strconv.FormatUint(toUint64(claim), 10)) 64 case float32: 65 attributes = append(attributes, key+"="+strconv.FormatFloat(float64(claim), 'G', -1, 32)) 66 case float64: 67 attributes = append(attributes, key+"="+strconv.FormatFloat(claim, 'G', -1, 64)) 68 case string: 69 attributes = append(attributes, key+"="+claim) 70 case []string: 71 for _, data := range claim { 72 attributes = append(attributes, key+"="+data) 73 } 74 case map[string]interface{}: 75 for ikey, ivalue := range claim { 76 for _, v := range FlattenClaim(ikey, ivalue) { 77 attributes = append(attributes, key+":"+v) 78 } 79 } 80 case []interface{}: 81 for _, value := range claim { 82 attributes = append(attributes, FlattenClaim(key, value)...) 83 } 84 default: 85 // do nothing, just return attributes 86 } 87 return attributes 88 }