Skip to content

Commit

Permalink
Expose v2/keys to get jwt public key (#184)
Browse files Browse the repository at this point in the history
* Expose v2/keys to get jwt public key descope/etc#1126

* A better implementaion for extracting the keys
replace /v2/ with const
  • Loading branch information
aviadl authored Dec 9, 2022
1 parent f12c9b4 commit deeb850
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 6 deletions.
6 changes: 4 additions & 2 deletions descope/api/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ const (

var (
Routes = endpoints{
version: "/v1/",
version: "/v1/",
versionV2: "/v2/",
auth: authEndpoints{
signInOTP: "auth/otp/signin",
signUpOTP: "auth/otp/signup",
Expand Down Expand Up @@ -95,6 +96,7 @@ var (

type endpoints struct {
version string
versionV2 string
auth authEndpoints
mgmt mgmtEndpoints
logout string
Expand Down Expand Up @@ -258,7 +260,7 @@ func (e *endpoints) Me() string {
return path.Join(e.version, e.me)
}
func (e *endpoints) GetKeys() string {
return path.Join(e.version, e.keys)
return path.Join(e.versionV2, e.keys)
}
func (e *endpoints) RefreshToken() string {
return path.Join(e.version, e.refresh)
Expand Down
4 changes: 2 additions & 2 deletions descope/auth/auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ func TestValidateSessionFetchKeyCalledOnce(t *testing.T) {
count := 0
a, err := newTestAuthConf(&AuthParams{ProjectID: "a"}, nil, mocks.Do(func(r *http.Request) (*http.Response, error) {
count++
return &http.Response{StatusCode: http.StatusOK, Body: io.NopCloser(strings.NewReader(fmt.Sprintf("[%s]", publicKey)))}, nil
return &http.Response{StatusCode: http.StatusOK, Body: io.NopCloser(strings.NewReader(fmt.Sprintf(`{"keys":[%s]}`, publicKey)))}, nil
}))
require.NoError(t, err)
ok, _, err := a.validateSession(jwtTokenValid, "", false, nil)
Expand All @@ -234,7 +234,7 @@ func TestValidateSessionFetchKeyCalledOnce(t *testing.T) {

func TestValidateSessionFetchKeyMalformed(t *testing.T) {
a, err := newTestAuthConf(&AuthParams{ProjectID: "a"}, nil, mocks.Do(func(r *http.Request) (*http.Response, error) {
return &http.Response{StatusCode: http.StatusOK, Body: io.NopCloser(strings.NewReader(fmt.Sprintf("[%s]", unknownPublicKey)))}, nil
return &http.Response{StatusCode: http.StatusOK, Body: io.NopCloser(strings.NewReader(fmt.Sprintf(`{"keys":[%s]}`, unknownPublicKey)))}, nil
}))
require.NoError(t, err)
ok, _, err := a.validateSession(jwtTokenValid, jwtTokenValid, false, nil)
Expand Down
5 changes: 3 additions & 2 deletions descope/auth/jwt.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,12 @@ func (p *provider) selectKey(sink jws.KeySink, key jwk.Key) error {

func (p *provider) requestKeys() error {
projectID := p.conf.ProjectID
keys := []map[string]interface{}{}
_, err := p.client.DoGetRequest(path.Join(api.Routes.GetKeys(), projectID), &api.HTTPRequest{ResBodyObj: &keys}, "")
keysWrapper := map[string][]map[string]interface{}{}
_, err := p.client.DoGetRequest(path.Join(api.Routes.GetKeys(), projectID), &api.HTTPRequest{ResBodyObj: &keysWrapper}, "")
if err != nil {
return err
}
keys := keysWrapper["keys"]
tempKeySet := map[string]jwk.Key{}
for i := range keys {
b, err := utils.Marshal(keys[i])
Expand Down

0 comments on commit deeb850

Please sign in to comment.