Skip to content

Commit

Permalink
fix: Cleanups [DEV-3050] (#307)
Browse files Browse the repository at this point in the history
* Get rid of `Authorize` button in Swagger UI

* Get rid of swagger `Authorize` from source code

* Add all the rest auth routes for API guarding

* Remove extra spaces in jsdoc swagger comments.

* Remove unused imports.

* Remove unused comments

* Get API guarding back:
- removed /credential-status/check
- removed /credential-status/search
- removed /did resolve the DID
- removed /presentation/verify

* Refactor auth flow and allow unauthorized users to make some actions

* Makes Identity initiating without race conditions

* Fix reassign condition

---------

Co-authored-by: abdulla-ashurov <abdulla.ashurov@dsr-corporation.com>
  • Loading branch information
Andrew Nikitin and abdulla-ashurov committed Aug 1, 2023
1 parent 9f775dd commit 2e6b969
Show file tree
Hide file tree
Showing 15 changed files with 279 additions and 182 deletions.
16 changes: 7 additions & 9 deletions src/controllers/credentials.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,13 +154,12 @@ export class CredentialController {
const { credential, policies } = request.body
const verifyStatus = request.query.verifyStatus === 'true' ? true : false
try {
const result = await Identity.instance.verifyCredential(
const result = await Identity.unauthorized.verifyCredential(
credential,
{
verifyStatus,
policies
},
response.locals.customerId
}
)
if (result.error) {
return response.status(400).json({
Expand Down Expand Up @@ -223,7 +222,7 @@ export class CredentialController {

const publish = request.query.publish === 'false' ? false : true
try {
return response.status(200).json(await Identity.instance.revokeCredentials(request.body.credential, publish, response.locals.customerId))
return response.status(200).json(await new Identity(response.locals.customerId).agent.revokeCredentials(request.body.credential, publish, response.locals.customerId))
} catch (error) {
return response.status(500).json({
error: `${error}`
Expand Down Expand Up @@ -275,7 +274,7 @@ export class CredentialController {
}

try {
return response.status(200).json(await Identity.instance.suspendCredentials(request.body.credential, request.body.publish, response.locals.customerId))
return response.status(200).json(await new Identity(response.locals.customerId).agent.suspendCredentials(request.body.credential, request.body.publish, response.locals.customerId))
} catch (error) {
return response.status(500).json({
error: `${error}`
Expand Down Expand Up @@ -327,7 +326,7 @@ export class CredentialController {
}

try {
return response.status(200).json(await Identity.instance.reinstateCredentials(request.body.credential, request.body.publish, response.locals.customerId))
return response.status(200).json(await new Identity(response.locals.customerId).agent.reinstateCredentials(request.body.credential, request.body.publish, response.locals.customerId))
} catch (error) {
return response.status(500).json({
error: `${error}`
Expand Down Expand Up @@ -388,14 +387,13 @@ export class CredentialController {
const { presentation, verifierDid, policies } = request.body
const verifyStatus = request.query.verifyStatus === 'true' ? true : false
try {
const result = await Identity.instance.verifyPresentation(
const result = await Identity.unauthorized.verifyPresentation(
presentation,
{
verifyStatus,
policies,
domain: verifierDid
},
response.locals.customerId
}
)
if (result.error) {
return response.status(400).json({
Expand Down
24 changes: 12 additions & 12 deletions src/controllers/issuer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ export class IssuerController {
*/
public async createKey(request: Request, response: Response) {
try {
const key = await Identity.instance.createKey('Ed25519', response.locals.customerId)
const key = await new Identity(response.locals.customerId).agent.createKey('Ed25519', response.locals.customerId)
return response.status(200).json(key)
} catch (error) {
return response.status(500).json({
Expand Down Expand Up @@ -141,7 +141,7 @@ export class IssuerController {
*/
public async getKey(request: Request, response: Response) {
try {
const key = await Identity.instance.getKey(request.params.kid, response.locals.customerId)
const key = await new Identity(response.locals.customerId).agent.getKey(request.params.kid, response.locals.customerId)
return response.status(200).json(key)
} catch (error) {
return response.status(500).json({
Expand Down Expand Up @@ -206,7 +206,7 @@ export class IssuerController {
if (request.body.didDocument) {
didDocument = request.body.didDocument
} else if (verificationMethodType) {
const key = await Identity.instance.createKey('Ed25519', response.locals.customerId)
const key = await new Identity(response.locals.customerId).agent.createKey('Ed25519', response.locals.customerId)
didDocument = generateDidDoc({
verificationMethod: verificationMethodType || VerificationMethods.Ed255192018,
verificationMethodId: 'key-1',
Expand All @@ -232,7 +232,7 @@ export class IssuerController {
})
}

const did = await Identity.instance.createDid(network || didDocument.id.split(':')[2], didDocument, response.locals.customerId)
const did = await new Identity(response.locals.customerId).agent.createDid(network || didDocument.id.split(':')[2], didDocument, response.locals.customerId)
return response.status(200).json(did)
} catch (error) {
return response.status(500).json({
Expand Down Expand Up @@ -286,7 +286,7 @@ export class IssuerController {
if (request.body.didDocument) {
updatedDocument = request.body.didDocument
} else if (did && (service || verificationMethod || authentication)) {
let resolvedResult = await Identity.instance.resolveDid(did)
let resolvedResult = await new Identity(response.locals.customerId).agent.resolveDid(did)
if(!resolvedResult?.didDocument || resolvedResult.didDocumentMetadata.deactivated) {
return response.status(400).send({
error: `${did} is either Deactivated or Not found`
Expand All @@ -310,7 +310,7 @@ export class IssuerController {
})
}

const result = await Identity.instance.updateDid(updatedDocument, response.locals.customerId)
const result = await new Identity(response.locals.customerId).agent.updateDid(updatedDocument, response.locals.customerId)
return response.status(200).json(result)
} catch (error) {
return response.status(500).json({
Expand Down Expand Up @@ -357,7 +357,7 @@ export class IssuerController {
}

try {
const did = await Identity.instance.deactivateDid(request.params.did, response.locals.customerId)
const did = await new Identity(response.locals.customerId).agent.deactivateDid(request.params.did, response.locals.customerId)
return response.status(200).json(did)
} catch (error) {
return response.status(500).json({
Expand Down Expand Up @@ -413,7 +413,7 @@ export class IssuerController {
let resourcePayload: Partial<MsgCreateResourcePayload> = {}
try {
// check if did is registered on the ledger
let resolvedDocument: any = await Identity.instance.resolveDid(did)
let resolvedDocument: any = await new Identity(response.locals.customerId).agent.resolveDid(did)
if(!resolvedDocument?.didDocument || resolvedDocument.didDocumentMetadata.deactivated) {
return response.status(400).send({
error: `${did} is a either Deactivated or Not found`
Expand All @@ -432,7 +432,7 @@ export class IssuerController {
alsoKnownAs
}
network = network || (did.split(':'))[2]
const result = await Identity.instance.createResource( network, resourcePayload, response.locals.customerId)
const result = await new Identity(response.locals.customerId).agent.createResource( network, resourcePayload, response.locals.customerId)
if ( result ) {
return response.status(201).json({
resource: resourcePayload
Expand Down Expand Up @@ -477,9 +477,9 @@ export class IssuerController {
try {
let did: any
if(request.params.did) {
did = await Identity.instance.resolveDid(request.params.did)
did = await new Identity(response.locals.customerId).agent.resolveDid(request.params.did)
} else {
did = await Identity.instance.listDids(response.locals.customerId)
did = await new Identity(response.locals.customerId).agent.listDids(response.locals.customerId)
}

return response.status(200).json(did)
Expand Down Expand Up @@ -523,7 +523,7 @@ export class IssuerController {
try {
let did: any
if(request.params.did) {
did = await Identity.instance.resolveDid(request.params.did)
did = await new Identity(response.locals.customerId).agent.resolveDid(request.params.did)
return response.status(200).json(did)
}
} catch (error) {
Expand Down
18 changes: 10 additions & 8 deletions src/controllers/revocation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,9 @@ export class RevocationController {
try {
let result: any
if (data) {
result = await Identity.instance.broadcastStatusList2021(did, { data, name: statusListName, alsoKnownAs, version: statusListVersion }, { encoding, statusPurpose }, response.locals.customerId)
result = await new Identity(response.locals.customerId).agent.broadcastStatusList2021(did, { data, name: statusListName, alsoKnownAs, version: statusListVersion }, { encoding, statusPurpose }, response.locals.customerId)
}
result = await Identity.instance.createStatusList2021(did, { name: statusListName, alsoKnownAs, version: statusListVersion }, { length, encoding, statusPurpose }, response.locals.customerId)
result = await new Identity(response.locals.customerId).agent.createStatusList2021(did, { name: statusListName, alsoKnownAs, version: statusListVersion }, { length, encoding, statusPurpose }, response.locals.customerId)
if (result.error) {
return response.status(400).json(result)
}
Expand Down Expand Up @@ -176,9 +176,9 @@ export class RevocationController {
try {
let result: any
if (data) {
result = await Identity.instance.broadcastStatusList2021(did, { data, name: statusListName, alsoKnownAs, version: statusListVersion }, { encoding, statusPurpose }, response.locals.customerId)
result = await new Identity(response.locals.customerId).agent.broadcastStatusList2021(did, { data, name: statusListName, alsoKnownAs, version: statusListVersion }, { encoding, statusPurpose }, response.locals.customerId)
}
result = await Identity.instance.createStatusList2021(did, { name: statusListName, alsoKnownAs, version: statusListVersion }, { length, encoding, statusPurpose }, response.locals.customerId)
result = await new Identity(response.locals.customerId).agent.createStatusList2021(did, { name: statusListName, alsoKnownAs, version: statusListVersion }, { length, encoding, statusPurpose }, response.locals.customerId)
if (result.error) {
return response.status(400).json(result)
}
Expand Down Expand Up @@ -339,8 +339,7 @@ export class RevocationController {
indices = typeof indices === 'number' ? [indices] : indices

try {
let result: any
result = await Identity.instance.updateStatusList2021(did, { indices, statusListName, statusListVersion, statusAction }, publish, response.locals.customerId)
const result = await new Identity(response.locals.customerId).agent.updateStatusList2021(did, { indices, statusListName, statusListVersion, statusAction }, publish, response.locals.customerId)
if (result.error) {
return response.status(400).json(result)
}
Expand Down Expand Up @@ -415,8 +414,11 @@ export class RevocationController {
const statusPurpose = request.query.statusPurpose as 'revocation' | 'suspension'

try {
let result: any
result = await Identity.instance.checkStatusList2021(did, { statusListIndex: index, statusListName, statusPurpose }, response.locals.customerId)
const result = await new Identity(response.locals.customerId).agent.checkStatusList2021(
did,
{ statusListIndex: index, statusListName, statusPurpose },
response.locals.customerId)

if (result.error) {
return response.status(400).json(result)
}
Expand Down
4 changes: 2 additions & 2 deletions src/middleware/auth/account-auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ export class AccountAuthHandler extends AbstractAuthHandler {

constructor () {
super()
this.registerRoute('/account', 'GET', 'read:account')
this.registerRoute('/account', 'POST', 'create:account')
this.registerRoute('/account', 'GET', 'read:account', { skipNamespace: true })
this.registerRoute('/account', 'POST', 'create:account', { skipNamespace: true })
}
public async handle(request: Request, response: Response): Promise<IAuthResponse> {
if (!request.path.includes('/account')) {
Expand Down

0 comments on commit 2e6b969

Please sign in to comment.