Skip to content
This repository has been archived by the owner on Apr 5, 2023. It is now read-only.

Commit

Permalink
feat: Validate and check Waci profile creation that each scope has ou…
Browse files Browse the repository at this point in the history
…tput descriptors configured.

closes #581

Signed-off-by: talwinder50 <[email protected]>
  • Loading branch information
talwinder50 committed Jan 27, 2022
1 parent 9ff8c67 commit 73027df
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 1 deletion.
4 changes: 3 additions & 1 deletion pkg/restapi/issuer/operation/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import (
)

// ProfileDataRequest req for profile creation.
// Issuer ID identifies who is the issuer of the credential manifests being issued.
// CMStyle represents an entity styles object as defined in credential manifest spec.
type ProfileDataRequest struct {
ID string `json:"id,omitempty"`
Name string `json:"name"`
Expand All @@ -26,7 +28,7 @@ type ProfileDataRequest struct {
SupportsWACI bool `json:"supportsWACI"`
OIDCProviderURL string `json:"oidcProvider"`
OIDCClientParams *OIDCClientParams `json:"oidcParams,omitempty"`
CredentialScopes []string `json:"scopes,omitempty"`
CredentialScopes []string `json:"credScopes,omitempty"`
LinkedWalletURL string `json:"linkedWallet,omitempty"`
IssuerID string `json:"issuerID,omitempty"`
CMStyle cm.Styles `json:"styles,omitempty"`
Expand Down
13 changes: 13 additions & 0 deletions pkg/restapi/issuer/operation/operations.go
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,7 @@ func New(config *Config) (*Operation, error) { // nolint:funlen,gocyclo,cyclop
refreshTokenStore: refreshStore,
didDomain: config.DidDomain,
jsonldDocLoader: config.JSONLDDocumentLoader,
cmOutputDescriptor: config.CmOutputDescriptor,
}

op.createOIDCClientFunc = op.getOrCreateOIDCClient
Expand Down Expand Up @@ -392,6 +393,18 @@ func (o *Operation) createIssuerProfileHandler(rw http.ResponseWriter, req *http
return
}

if profileData.SupportsWACI {
for _, pCredScope := range profileData.CredentialScopes {
if _, ok := o.cmOutputDescriptor[pCredScope]; !ok {
commhttp.WriteErrorResponseWithLog(rw, http.StatusInternalServerError,
fmt.Sprintf("failed to get output descriptors configured for waci "+
"profile(s)"), profileEndpoint, logger)

return
}
}
}

if profileData.OIDCProviderURL != "" {
_, err = o.createOIDCClientFunc(profileData)
if err != nil {
Expand Down
69 changes: 69 additions & 0 deletions pkg/restapi/issuer/operation/operations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -551,6 +551,50 @@ func TestCreateProfile(t *testing.T) {
require.Equal(t, vReq.SupportsAssuranceCredential, profileRes.SupportsAssuranceCredential)
})

t.Run("create waci profile with cm output descriptors - success", func(t *testing.T) {
t.Parallel()

vReq := createProfileData(uuid.New().String())
vReq.SupportsWACI = true
vReq.CredentialScopes = []string{mockCredScope}
vReq.OIDCClientParams = &issuer.OIDCClientParams{
ClientID: "client id",
ClientSecret: "client secret",
SecretExpiry: 0,
}

vReqBytes, err := json.Marshal(vReq)
require.NoError(t, err)

op, err := New(config(t))
require.NoError(t, err)

op.cmOutputDescriptor = map[string][]*cm.OutputDescriptor{
mockCredScope: {
&cm.OutputDescriptor{
ID: uuid.New().String(),
Schema: "https://www.w3.org/2018/credentials/examples/v1",
},
},
}

h := getHandler(t, op, endpoint)
rr := serveHTTP(t, h.Handle(), http.MethodPost, endpoint, vReqBytes)

require.Equal(t, http.StatusCreated, rr.Code)

profileRes := &issuer.ProfileData{}
err = json.Unmarshal(rr.Body.Bytes(), &profileRes)
require.NoError(t, err)
require.Equal(t, vReq.ID, profileRes.ID)
require.Equal(t, vReq.Name, profileRes.Name)
require.Equal(t, vReq.URL, profileRes.URL)
require.Equal(t, vReq.SupportsAssuranceCredential, profileRes.SupportsAssuranceCredential)
require.Equal(t, vReq.CredentialScopes, profileRes.CredentialScopes)
require.Equal(t, vReq.IssuerID, profileRes.IssuerID)
require.Equal(t, vReq.SupportsWACI, profileRes.SupportsWACI)
})

t.Run("create profile - failed to issue governance vc", func(t *testing.T) {
t.Parallel()

Expand Down Expand Up @@ -680,6 +724,31 @@ func TestCreateProfile(t *testing.T) {

require.Contains(t, resErr.ErrMessage, "create oidc client")
})
t.Run("create profile - failed to output descriptors configured for waci profiles", func(t *testing.T) {
t.Parallel()

ops, err := New(config(t))
require.NoError(t, err)

vReq := createProfileData(uuid.New().String())
vReq.SupportsWACI = true
vReq.CredentialScopes = []string{mockCredScope}

vReqBytes, err := json.Marshal(vReq)
require.NoError(t, err)

rr := serveHTTP(t, getHandler(t, ops, endpoint).Handle(), http.MethodPost, endpoint, vReqBytes)

require.Equal(t, http.StatusInternalServerError, rr.Code)

resErr := struct {
ErrMessage string `json:"errMessage"`
}{}
err = json.Unmarshal(rr.Body.Bytes(), &resErr)
require.NoError(t, err)

require.Contains(t, resErr.ErrMessage, "failed to get output descriptors configured for waci profile")
})
}

func TestGetProfile(t *testing.T) {
Expand Down

0 comments on commit 73027df

Please sign in to comment.