CheckPermissions is used for Admin authentication to check a CBS RBAC user. It performs two jobs: Authentication and then attempts Authorization. For Authorization it checks whether the user has any ONE of the supplied accessPermissions If the user is authorized it will also check the responsePermis
(ctx context.Context, httpClient *http.Client, managementEndpoints []string, bucketName, username, password string, accessPermissions []Permission, responsePermissions []Permission)
| 2005 | // If the user is authorized it will also check the responsePermissions and return the results for these. These can be |
| 2006 | // used by handlers to determine different responses based on the permissions the user has. |
| 2007 | func CheckPermissions(ctx context.Context, httpClient *http.Client, managementEndpoints []string, bucketName, username, password string, accessPermissions []Permission, responsePermissions []Permission) (statusCode int, permissionResults map[string]bool, err error) { |
| 2008 | combinedPermissions := append(accessPermissions, responsePermissions...) |
| 2009 | body := []byte(strings.Join(FormatPermissionNames(combinedPermissions, bucketName), ",")) |
| 2010 | statusCode, bodyResponse, err := doHTTPAuthRequest(ctx, httpClient, username, password, "POST", "/pools/default/checkPermissions", managementEndpoints, body) |
| 2011 | if err != nil { |
| 2012 | return http.StatusInternalServerError, nil, err |
| 2013 | } |
| 2014 | |
| 2015 | if statusCode != http.StatusOK { |
| 2016 | if statusCode == http.StatusUnauthorized { |
| 2017 | return http.StatusUnauthorized, nil, nil |
| 2018 | } |
| 2019 | |
| 2020 | // If we don't provide permissions we get a BadRequest but know we have successfully authenticated |
| 2021 | if statusCode == http.StatusBadRequest && len(combinedPermissions) > 0 { |
| 2022 | return statusCode, nil, nil |
| 2023 | } |
| 2024 | } |
| 2025 | |
| 2026 | // At this point we know the user exists, now check whether they have the required permissions |
| 2027 | if len(combinedPermissions) > 0 { |
| 2028 | var permissions map[string]bool |
| 2029 | |
| 2030 | err = base.JSONUnmarshal(bodyResponse, &permissions) |
| 2031 | if err != nil { |
| 2032 | return http.StatusInternalServerError, nil, err |
| 2033 | } |
| 2034 | |
| 2035 | if len(responsePermissions) > 0 { |
| 2036 | permissionResults = make(map[string]bool) |
| 2037 | for _, responsePermission := range responsePermissions { |
| 2038 | hasPermission, ok := permissions[responsePermission.FormattedName(bucketName)] |
| 2039 | // This should always be true but better to be safe to avoid panic |
| 2040 | if ok { |
| 2041 | permissionResults[responsePermission.PermissionName] = hasPermission |
| 2042 | } |
| 2043 | } |
| 2044 | } |
| 2045 | |
| 2046 | for _, accessPermission := range accessPermissions { |
| 2047 | if hasPermission, ok := permissions[accessPermission.FormattedName(bucketName)]; ok && hasPermission { |
| 2048 | return http.StatusOK, permissionResults, nil |
| 2049 | } |
| 2050 | } |
| 2051 | } |
| 2052 | |
| 2053 | return http.StatusForbidden, nil, nil |
| 2054 | } |
| 2055 | |
| 2056 | type WhoAmIResponse struct { |
| 2057 | Roles []struct { |