Skip to content

Commit

Permalink
feat: create user on auth server
Browse files Browse the repository at this point in the history
  • Loading branch information
Salaton committed Jan 20, 2023
1 parent cd9c4e3 commit 7322e95
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 7 deletions.
54 changes: 47 additions & 7 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func NewClient(config Config) (*Client, error) {
},
}

err = client.Authenticate()
_, err = client.Authenticate()
if err != nil {
return nil, fmt.Errorf("unable to initialize server client: %w", err)
}
Expand All @@ -77,7 +77,7 @@ func NewClient(config Config) (*Client, error) {
}

// Authenticate uses client credentials to log in to a slade360 authentication server
func (c *Client) Authenticate() error {
func (c *Client) Authenticate() (*LoginResponse, error) {
apiTokenURL := fmt.Sprintf("%s/oauth2/token/", c.configurations.AuthServerEndpoint)
credentials := url.Values{}
credentials.Set("client_id", c.configurations.ClientID)
Expand All @@ -90,21 +90,51 @@ func (c *Client) Authenticate() error {

response, err := c.client.Post(apiTokenURL, "application/x-www-form-urlencoded", encodedCredentials)
if err != nil {
return err
return nil, err
}

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

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

return nil
return &responseData, nil
}

// 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)
if err != nil {
return nil, err
}

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

if response.StatusCode >= 300 || response.StatusCode < 200 {
msg := fmt.Sprintf(
"error from create user endpoint, status %d and error: %s",
response.StatusCode, string(data),
)
return nil, fmt.Errorf(msg)
}

var dataResponse *CreateUserResponse
err = json.Unmarshal(data, &dataResponse)
if err != nil {
return nil, err
}

return dataResponse, nil
}

// verifyAccessToken is used to introspect a token to determine the active state of the
Expand All @@ -124,7 +154,7 @@ func (c *Client) verifyAccessToken(ctx context.Context, accessToken string) (*To
Token: accessToken,
}

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

Expand All @@ -185,6 +216,15 @@ func (c *Client) makeRequest(
return nil, err
}

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

req.Header.Set("Authorization", token)
}
req.Header.Set("Accept", "application/json")
req.Header.Set("Content-Type", contentType)

Expand Down
31 changes: 31 additions & 0 deletions models.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,34 @@ type TokenIntrospectionPayload struct {
TokenType string `json:"token_type"`
Token string `json:"token"`
}

// CreateUserPayload defines the object passed when creating a user on authserver
type CreateUserPayload struct {
Firstname string `json:"first_name"`
Lastname string `json:"last_name"`
Othernames string `json:"other_names"`
Email string `json:"email"`
IsActive bool `json:"is_active"`
NewPassword string `json:"password"`
ConfirmPassword string `json:"confirm_password"`
Organisation string `json:"organisation"`
AgreedToTerms bool `json:"agreed_to_terms"`
}

// CreateUserResponse defines the json object returned when a user is successfully created on Slade360 Auth Server
type CreateUserResponse 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"`
DateJoined time.Time `json:"date_joined"`
AgreedToTerms bool `json:"agreed_to_terms"`
LastPasswordChange time.Time `json:"last_password_change"`
BusinessPartner string `json:"business_partner"`
LastLogin time.Time `json:"last_login"`
UserRoles []interface{} `json:"user_roles"`
}

0 comments on commit 7322e95

Please sign in to comment.