Through JavaScript
- Last Updated: September 10, 2026
- 2 minute read
- MarkLogic Server
- Version 12.0
- Documentation
Note:
Run all code against the MarkLogic Server Security database.
To set up OAuth-based authentication and authorization with Amazon Cognito using JavaScript through the Query Console, follow these steps:
-
Create the external security object by executing code like this:
Note:
The JWT Secrets field secures both symmetric and asymmetric signature keys.Note:
You can specify a JWKS URI to validate incoming JWT access tokens with JWKS instead of with JWT Secrets signature keys.Note:
The parametersoauthAuthorizationServerUri,oauthTokenServerUri,oauthRedirectUri,oauthScope,oauthClientAuthenticationMethod, andoauthClientSecretare required only when using the Authorization code flow. These parameters configure the MarkLogic app server to act as an OAuth client. They are not needed for the Resource server flow.Resource server flow
declareUpdate(); const sec = require('/MarkLogic/security'); const oauthVendor = "Amazon Cognito"; const oauthFlowType = "Resource server"; const oauthClientId = "19vomjilg46bbvcpp9qcmeacoc"; const oauthTokenType = "JSON Web Tokens"; const oauthUsernameAttribute = "username"; const oauthRoleAttribute = "cognito:groups"; const oauthJWTAlg = "RS256"; const oauthJWTIssuerUri = "https://cognito-idp.us-east-1.amazonaws.com/us-east-1_fMQqTCMd9"; const oauthJWTKeyIds = "fBwvWl/oWKPB9fyhXtZ8EqAhAmljMhk4hW2dd/zpFYs="; const oauthJWTSecretValues = "-----BEGIN PUBLIC KEY-----<PEM-converted RS256 Secret Value>-----END PUBLIC KEY-----"; const oauth = sec.oauthServer( oauthVendor, oauthFlowType, oauthClientId, oauthTokenType, oauthUsernameAttribute, oauthRoleAttribute, "", oauthJWTIssuerUri, oauthJWTAlg, oauthJWTKeyIds, oauthJWTSecretValues ); sec.createExternalSecurity( "AmazonCognitoExampleOAuth", "Amazon Cognito external security object for OAuth", "oauth", 300, "oauth", null, null, oauth);Authorization code flow
declareUpdate(); const sec = require('/MarkLogic/security'); const oauthVendor = "Amazon Cognito"; const oauthFlowType = "Authorization code"; const oauthClientId = "<application-client-id>"; const oauthTokenType = "JSON Web Tokens"; const oauthUsernameAttribute = "username"; const oauthRoleAttribute = "cognito:groups"; const oauthJWTAlg = "RS256"; const oauthJWTIssuerUri = "<jwt-issuer-uri>"; const oauthJWTKeyIds = "<key-id>"; const oauthJWTSecretValues = "-----BEGIN PUBLIC KEY-----<PEM-converted key>-----END PUBLIC KEY-----"; const oauthJwksUri = ""; const oauthAuthorizationServerUri = "<oauth-server-authorize-endpoint>"; const oauthTokenServerUri = "<oauth-server-token-endpoint>"; const oauthRedirectUri = "https://<marklogic-host>:<app-server-port>"; const oauthScope = "openid profile"; const oauthClientAuthenticationMethod = "Client secret"; const oauthClientSecret = "<client-secret-value>"; const oauth = sec.oauthServer( oauthVendor, oauthFlowType, oauthClientId, oauthTokenType, oauthUsernameAttribute, oauthRoleAttribute, "", oauthJWTIssuerUri, oauthJWTAlg, oauthJWTKeyIds, oauthJWTSecretValues, oauthJwksUri, oauthAuthorizationServerUri, oauthTokenServerUri, oauthRedirectUri, oauthScope, oauthClientAuthenticationMethod, oauthClientSecret ); sec.createExternalSecurity( "AmazonCognitoExampleOAuthClient", "Amazon Cognito external security object for OAuth Authorization Code flow", "oauth", 300, "oauth", null, null, oauth); -
Create any HTTP, XDBC, WebDAV, or ODBC app servers that you wish to configure with this external security object.
-
Configure your app servers to use this external security object with code like this:
declareUpdate(); const admin = require('/MarkLogic/admin.xqy'); const config = admin.getConfiguration(); const groupid = admin.groupGetId(config, "Default"); const appserver = "<app server name>"; const extsec = "<external security object name>"; admin.saveConfiguration(admin.appserverSetExternalSecurity(config, groupid, admin.appServerGetId(config, appserver), extsec, fn.false(), "oauth")); -
Assign external names to your desired roles with code like this:
declareUpdate(); const sec = require('/MarkLogic/security.xqy'); const roleName = "<MarkLogic Server role name, for example, manage-user>"; const externalName = "GroupFoo"; sec.roleSetExternalNames(roleName, externalName);
MarkLogic Server is now set up for OAuth-based authentication and authorization with Amazon Cognito.