Skip to content

feat: add Ims token for db commands#36

Open
AjazSumaiya wants to merge 5 commits intoepic/abdb-implementationfrom
cext-5606/feat-add-Imstoken-auth
Open

feat: add Ims token for db commands#36
AjazSumaiya wants to merge 5 commits intoepic/abdb-implementationfrom
cext-5606/feat-add-Imstoken-auth

Conversation

@AjazSumaiya
Copy link
Collaborator

Description

This PR changes DBBase Command to rely on IMS-lib to fetch or reuse IMS tokens for Ims authentication for db service

Changes done -

  • DBBaseCommand calls authHelper.getAccessToken() which internally uses Ims lib
  • authHelper.getAccessToken() checks ims.contexts.<runtime_namespace> for an existing token
  • If the token is valid, it is reused; otherwise it calls ims.getAccessTokenByClientCredentials()
  • The generated token is stored back in local ims context config ims.contexts.<namespace>.token

Related Issue

Motivation and Context

How Has This Been Tested?

On local

Screenshots (if appropriate):

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

Checklist:

  • I have signed the Adobe Open Source CLA.
  • My code follows the code style of this project.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have read the CONTRIBUTING document.
  • I have added tests to cover my changes.
  • All new and existing tests passed.

Copy link
Collaborator

@nofuss nofuss left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Question about token expiration.

if (currentToken) {
const validToken = await ims.validateToken(currentToken)
if (validToken.valid) {
return currentToken
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there any chance that even if a token is "valid" that it might be expired? In other words, is there any need to generate a new token after some period of time?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, so token has expiration of 24 hrs, so if the token stored in local context expires, we need to generate new in that case

Copy link
Member

@moritzraho moritzraho Mar 1, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

aio-lib-ims already takes care of refreshing the token. Here what we should do is similar to https://github.com/adobe/aio-cli-plugin-app/blob/d28deaf0b2f8c973bf67fadea986834cdf143e50/src/lib/auth-helper.js#L36

but instead of using the CLI context (reserved context for user tokens, that triggers a SUSI login) we should set the context to the IMS_OAUTH_S2S env vars. See the quickstart in https://github.com/adobe/aio-lib-ims?tab=readme-ov-file#quickstart

bottom line:

  • we should not manipulate the config ourselves, which is manipulated internally by the aio-lib-ims context concept
  • we don't need to refresh the token

Copy link
Member

@moritzraho moritzraho Mar 1, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One more thing, there is a caveat in the aio-lib-ims plugin system which will require us to set also a technical_account_email and technical_account_id in the context, this is needed so that aio-lib-ims recognizes the credential as a client credential for the oauth s2s flow. You should be able to generate the token by setting dummy values for those two fields.

https://github.com/adobe/aio-lib-ims-oauth/blob/master/src/ims-oauth_server_to_server.js#L31C1-L32C28

Comment on lines 61 to 71
if (tokenResponse.payload?.access_token) {
return tokenResponse.payload.access_token
}

if (tokenResponse.access_token) {
return tokenResponse.access_token
}

if (typeof tokenResponse === 'string') {
return tokenResponse
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we not know the format of the response?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

checked response format and fixed

const runtimeNamespace = config.get(CONFIG_RUNTIME_NAMESPACE)

if (!runtimeNamespace) {
throw new Error('Runtime namespace is required. Please set CONFIG_RUNTIME_NAMESPACE.')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is env AIO_RUNTIME_NAMESPACE

(this happens when the local project is not synced with console)

@@ -0,0 +1,142 @@
/*
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Most of this logic is already handled by aio-lib-ims

please rewrite accordingly to https://github.com/adobe/aio-cli-plugin-app-storage/pull/36/changes#r2869855342

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants