Skip to content

Commit

Permalink
feat: add validate user endpoint (#19)
Browse files Browse the repository at this point in the history
  • Loading branch information
robinmuhia authored Jul 16, 2024
1 parent 21eac88 commit f93e5be
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 5 deletions.
44 changes: 39 additions & 5 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ func (c *Client) Authenticate() (*OAUTHResponse, error) {
// CreateUser creates a user on slade360 auth server
func (c *Client) CreateUser(ctx context.Context, input *CreateUserPayload) (*CreateUserResponse, error) {
createUserEndpoint := fmt.Sprintf("%s/v1/user/user_roles/", c.configurations.AuthServerEndpoint)
response, err := c.makeRequest(ctx, http.MethodPost, createUserEndpoint, input, "application/json", true)
response, err := c.makeRequest(ctx, http.MethodPost, createUserEndpoint, input, "application/json", true, nil)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -190,6 +190,37 @@ func (c *Client) LoginUser(ctx context.Context, input *LoginUserPayload) (*OAUTH
return responseData, nil
}

// ValidateUser validates whether a user exists on the authserver
func (c *Client) ValidateUser(ctx context.Context, authTokens *OAUTHResponse) (*MeResponse, error) {
meURL := fmt.Sprintf("%s/v1/user/me/", c.configurations.AuthServerEndpoint)

resp, err := c.makeRequest(ctx, http.MethodGet, meURL, nil, "application/json", true, authTokens)
if err != nil {
return nil, err
}

data, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}

if resp.StatusCode != 200 {
msg := fmt.Sprintf(
"an error occurred while processing your request. detail: %v",
string(data),
)
return nil, fmt.Errorf(msg)
}

var responseData MeResponse
err = json.Unmarshal(data, &responseData)
if err != nil {
return nil, err
}

return &responseData, nil
}

// verifyAccessToken is used to introspect a token to determine the active state of the
// OAuth 2.0 access token and to determine meta-information about this token.
func (c *Client) verifyAccessToken(ctx context.Context, accessToken string) (*TokenIntrospectionResponse, error) {
Expand All @@ -207,7 +238,7 @@ func (c *Client) verifyAccessToken(ctx context.Context, accessToken string) (*To
Token: accessToken,
}

response, err := c.makeRequest(ctx, http.MethodPost, introspectionURL, payload, "application/json", false)
response, err := c.makeRequest(ctx, http.MethodPost, introspectionURL, payload, "application/json", false, nil)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -255,6 +286,7 @@ func (c *Client) makeRequest(
body interface{},
contentType string,
isAuthenticated bool,
loginCreds *OAUTHResponse,
) (*http.Response, error) {
client := http.Client{}

Expand All @@ -270,9 +302,11 @@ func (c *Client) makeRequest(
}

if isAuthenticated {
loginCreds, err := c.Authenticate()
if err != nil {
return nil, err
if loginCreds == nil {
loginCreds, err = c.Authenticate()
if err != nil {
return nil, err
}
}
token := fmt.Sprintf("Bearer %s", loginCreds.AccessToken)

Expand Down
13 changes: 13 additions & 0 deletions models.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,19 @@ type OAUTHResponse struct {
TokenType string `json:"token_type"`
}

// MeResponse defines the object returned when a user's existence is validated on the authserver
type MeResponse struct {
ID int `json:"id"`
GUID string `json:"guid"`
Email string `json:"email"`
FirstName string `json:"first_name"`
LastName string `json:"last_name"`
OtherNames string `json:"other_names"`
IsStaff bool `json:"is_staff"`
IsActive bool `json:"is_active"`
Permissions []string `json:"permissions"`
}

// LoginPayload defines the payload passed when logging in to slade 360 auth server
type LoginPayload struct {
GrantType string `json:"grant_type"`
Expand Down

0 comments on commit f93e5be

Please sign in to comment.