ParseStringSliceClaim parses the claim for groups and roles, expected []string. Some providers like ADFS return a single string instead of an array if there is only 1 element. So this function handles the edge cases.
(claim interface{})
| 178 | // Some providers like ADFS return a single string instead of an array if there |
| 179 | // is only 1 element. So this function handles the edge cases. |
| 180 | func ParseStringSliceClaim(claim interface{}) ([]string, error) { |
| 181 | groups := make([]string, 0) |
| 182 | if claim == nil { |
| 183 | return groups, nil |
| 184 | } |
| 185 | |
| 186 | // The simple case is the type is exactly what we expected |
| 187 | asStringArray, ok := claim.([]string) |
| 188 | if ok { |
| 189 | cpy := make([]string, len(asStringArray)) |
| 190 | copy(cpy, asStringArray) |
| 191 | return cpy, nil |
| 192 | } |
| 193 | |
| 194 | asArray, ok := claim.([]interface{}) |
| 195 | if ok { |
| 196 | for i, item := range asArray { |
| 197 | asString, ok := item.(string) |
| 198 | if !ok { |
| 199 | return nil, xerrors.Errorf("invalid claim type. Element %d expected a string, got: %T", i, item) |
| 200 | } |
| 201 | groups = append(groups, asString) |
| 202 | } |
| 203 | return groups, nil |
| 204 | } |
| 205 | |
| 206 | asString, ok := claim.(string) |
| 207 | if ok { |
| 208 | if asString == "" { |
| 209 | // Empty string should be 0 groups. |
| 210 | return []string{}, nil |
| 211 | } |
| 212 | // If it is a single string, first check if it is a csv. |
| 213 | // If a user hits this, it is likely a misconfiguration and they need |
| 214 | // to reconfigure their IDP to send an array instead. |
| 215 | if strings.Contains(asString, ",") { |
| 216 | return nil, xerrors.Errorf("invalid claim type. Got a csv string (%q), change this claim to return an array of strings instead.", asString) |
| 217 | } |
| 218 | return []string{asString}, nil |
| 219 | } |
| 220 | |
| 221 | // Not sure what the user gave us. |
| 222 | return nil, xerrors.Errorf("invalid claim type. Expected an array of strings, got: %T", claim) |
| 223 | } |
| 224 | |
| 225 | // IsHTTPError handles us being inconsistent with returning errors as values or |
| 226 | // pointers. |