Browse Source

[CLOUDTRUST-1860] whitelisting errors of a certain format

master
Sonia 6 years ago
committed by GitHub
parent
commit
07fd6f0afb
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 61
      keycloak_client.go

61
keycloak_client.go

@ -3,6 +3,7 @@ package keycloak
import ( import (
"context" "context"
"encoding/json" "encoding/json"
"regexp"
"strconv" "strconv"
"fmt" "fmt"
@ -178,15 +179,7 @@ func (c *Client) get(accessToken string, data interface{}, plugins ...plugin.Plu
Message: string(resp.Bytes()), Message: string(resp.Bytes()),
} }
case resp.StatusCode >= 400: case resp.StatusCode >= 400:
var response map[string]string return treatErrorStatus(resp)
err := json.Unmarshal(resp.Bytes(), &response)
if message, ok := response["errorMessage"]; ok && err == nil {
return whitelistErrors(resp.StatusCode, message)
}
return HTTPError{
HTTPStatus: resp.StatusCode,
Message: string(resp.Bytes()),
}
case resp.StatusCode >= 200: case resp.StatusCode >= 200:
switch resp.Header.Get("Content-Type") { switch resp.Header.Get("Content-Type") {
case "application/json": case "application/json":
@ -228,15 +221,7 @@ func (c *Client) post(accessToken string, data interface{}, plugins ...plugin.Pl
Message: string(resp.Bytes()), Message: string(resp.Bytes()),
} }
case resp.StatusCode >= 400: case resp.StatusCode >= 400:
var response map[string]string return "", treatErrorStatus(resp)
err := json.Unmarshal(resp.Bytes(), &response)
if message, ok := response["errorMessage"]; ok && err == nil {
return "", whitelistErrors(resp.StatusCode, message)
}
return "", HTTPError{
HTTPStatus: resp.StatusCode,
Message: string(resp.Bytes()),
}
case resp.StatusCode >= 200: case resp.StatusCode >= 200:
var location = resp.Header.Get("Location") var location = resp.Header.Get("Location")
@ -280,15 +265,7 @@ func (c *Client) delete(accessToken string, plugins ...plugin.Plugin) error {
Message: string(resp.Bytes()), Message: string(resp.Bytes()),
} }
case resp.StatusCode >= 400: case resp.StatusCode >= 400:
var response map[string]string return treatErrorStatus(resp)
err := json.Unmarshal(resp.Bytes(), &response)
if message, ok := response["errorMessage"]; ok && err == nil {
return whitelistErrors(resp.StatusCode, message)
}
return HTTPError{
HTTPStatus: resp.StatusCode,
Message: string(resp.Bytes()),
}
case resp.StatusCode >= 200: case resp.StatusCode >= 200:
return nil return nil
default: default:
@ -325,15 +302,7 @@ func (c *Client) put(accessToken string, plugins ...plugin.Plugin) error {
Message: string(resp.Bytes()), Message: string(resp.Bytes()),
} }
case resp.StatusCode >= 400: case resp.StatusCode >= 400:
var response map[string]string return treatErrorStatus(resp)
err := json.Unmarshal(resp.Bytes(), &response)
if message, ok := response["errorMessage"]; ok && err == nil {
return whitelistErrors(resp.StatusCode, message)
}
return HTTPError{
HTTPStatus: resp.StatusCode,
Message: string(resp.Bytes()),
}
case resp.StatusCode >= 200: case resp.StatusCode >= 200:
return nil return nil
default: default:
@ -419,12 +388,26 @@ func str(s string) *string {
return &s return &s
} }
func treatErrorStatus(resp *gentleman.Response) error {
var response map[string]interface{}
err := json.Unmarshal(resp.Bytes(), &response)
if message, ok := response["errorMessage"]; ok && err == nil {
return whitelistErrors(resp.StatusCode, message.(string))
}
return HTTPError{
HTTPStatus: resp.StatusCode,
Message: string(resp.Bytes()),
}
}
func whitelistErrors(statusCode int, message string) error { func whitelistErrors(statusCode int, message string) error {
// whitelist errors from Keycloak // whitelist errors from Keycloak
reg := regexp.MustCompile("invalidPassword[a-zA-Z]*Message")
switch message { switch {
//POST account/credentials/password with error message "invalidPasswordExistingMessage" //POST account/credentials/password with error message related to invalid value for the password
case "invalidPasswordExistingMessage": // of the format invalidPassword{a-zA-Z}*Message, e.g. invalidPasswordMinDigitsMessage
case reg.MatchString(message):
return commonhttp.Error{ return commonhttp.Error{
Status: statusCode, Status: statusCode,
Message: "keycloak." + message, Message: "keycloak." + message,

Loading…
Cancel
Save