Skip to content

Commit

Permalink
feat: support username for get account
Browse files Browse the repository at this point in the history
  • Loading branch information
ahme-dev committed Jan 27, 2025
1 parent bbc43a8 commit 76d8bf6
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 20 deletions.
6 changes: 3 additions & 3 deletions authn/authn.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,9 @@ func (ac *Client) claimsFromVerifier(idToken string, verifier JWTClaimsExtractor
return claims, nil
}

// GetAccount gets the account with the associated id
func (ac *Client) GetAccount(id string) (*Account, error) { // Should this be a string or an int?
return ac.iclient.GetAccount(id)
// GetAccount gets the account with the associated id or username
func (ac *Client) GetAccount(idOrUsername string) (*Account, error) {
return ac.iclient.GetAccount(idOrUsername)
}

// Update updates the account with the associated id
Expand Down
6 changes: 3 additions & 3 deletions authn/internal_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,9 @@ func (ic *internalClient) Key(kid string) ([]jose.JSONWebKey, error) {
return jwks.Key(kid), nil
}

// GetAccount gets the account details for the specified account id
func (ic *internalClient) GetAccount(id string) (*Account, error) {
resp, err := ic.doWithAuth(get, "accounts/"+id, nil)
// GetAccount gets the account details for the specified account id or username
func (ic *internalClient) GetAccount(idOrUsername string) (*Account, error) {
resp, err := ic.doWithAuth(get, "accounts/"+idOrUsername, nil)
if err != nil {
return nil, err
}
Expand Down
44 changes: 30 additions & 14 deletions authn/internal_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,10 @@ func testingHTTPClient(handler http.Handler) (*http.Client, func()) {
// Based on information at https://keratin.github.io/authn-server/#/api?id=get-account
func TestICGetAccount(t *testing.T) {
type request struct {
url string
htusername string
htpassword string
id string
url string
htusername string
htpassword string
idOrUsername string
}
type response struct {
id int
Expand All @@ -71,10 +71,10 @@ func TestICGetAccount(t *testing.T) {
}{
{
request: request{
url: "http://test.com",
htusername: "username",
htpassword: "password",
id: "1",
url: "http://test.com",
htusername: "username",
htpassword: "password",
idOrUsername: "1",
},
response: response{
id: 1,
Expand All @@ -87,10 +87,26 @@ func TestICGetAccount(t *testing.T) {
},
{
request: request{
url: "http://test.com",
htusername: "username",
htpassword: "password",
id: "1",
url: "http://test.com",
htusername: "username",
htpassword: "password",
idOrUsername: "[email protected]",
},
response: response{
id: 1,
username: "[email protected]",
locked: true,
deleted: true,
code: http.StatusOK,
errorMsg: "",
},
},
{
request: request{
url: "http://test.com",
htusername: "username",
htpassword: "password",
idOrUsername: "1",
},
response: response{
code: http.StatusNotFound,
Expand All @@ -106,7 +122,7 @@ func TestICGetAccount(t *testing.T) {
assert.Equal(t, http.MethodGet, r.Method)
assert.Equal(t, tc.request.htusername, username)
assert.Equal(t, tc.request.htpassword, password)
assert.Equal(t, "/accounts/"+tc.request.id, r.URL.Path)
assert.Equal(t, "/accounts/"+tc.request.idOrUsername, r.URL.Path)
w.WriteHeader(tc.response.code)
//if we're mocking a good request, return the json
if tc.response.code == http.StatusOK {
Expand All @@ -129,7 +145,7 @@ func TestICGetAccount(t *testing.T) {
}
cli.client = httpClient

account, err := cli.GetAccount(tc.request.id)
account, err := cli.GetAccount(tc.request.idOrUsername)
if tc.response.errorMsg == "" { //Expecting no error
assert.Nil(t, err)
assert.Equal(t, tc.response.id, account.ID)
Expand Down

0 comments on commit 76d8bf6

Please sign in to comment.