52 lines
920 B
Go
52 lines
920 B
Go
package keycloak
|
|
|
|
import (
|
|
"errors"
|
|
"net/http"
|
|
|
|
"github.com/Nerzal/gocloak/v13"
|
|
)
|
|
|
|
type Config struct {
|
|
Env string
|
|
AuthServerAddr string
|
|
Realm string
|
|
ClientId string
|
|
ClientSecret string
|
|
}
|
|
|
|
func New(c *Config) (*Client, error) {
|
|
cli := &Client{
|
|
client: gocloak.NewClient(c.AuthServerAddr),
|
|
jwtManager: NewJWTManager(),
|
|
|
|
env: c.Env,
|
|
realm: c.Realm,
|
|
clientId: c.ClientId,
|
|
clientSecret: c.ClientSecret,
|
|
}
|
|
|
|
return cli, nil
|
|
}
|
|
|
|
func (k *Client) handleError(err error) error {
|
|
var apiErr *gocloak.APIError
|
|
|
|
if !errors.As(err, &apiErr) {
|
|
return ErrInternal
|
|
}
|
|
|
|
switch apiErr.Code {
|
|
case http.StatusNotFound:
|
|
return ErrRealmClientNotFound
|
|
case http.StatusUnauthorized:
|
|
return ErrRealmClientUnauthorized
|
|
case http.StatusConflict:
|
|
return ErrAlreadyExists
|
|
case http.StatusBadRequest:
|
|
return ErrBadRequest
|
|
default:
|
|
return ErrInternal
|
|
}
|
|
}
|