@adsk-sdk/auth v0.0.2
(Preview Version) NodeJS OAuth server side SDK
Auth SDK for NodeJs applications using 2 Legged and 3 Legged authentication . This code runs in Javascript and Typescript
Overview
This Node.js Client enables you to easily add authentication into your Forge app
Requirements
Install Node.js version 4 and above. Create an app on the Forge Developer portal. Note the client ID and client secret.
Installation
Using npm:
npm install @adsk-sdk/auth
Using yarn:
yarn add @adsk-sdk/auth
Getting Started
Creating the client
Create an AuthClient
instance before rendering or initializing your application. You should only have one instance of the client.
Typescript Instance
import { AuthSDK } from '@adsk-sdk/auth';
const auth = new AuthSDK({
clientId: `<AUTH_CLIENT_ID>`,
clientSecret: `<AUTH_CLIENT_ID>`,
env: `<AUTH_ENV_APP>`, // Just 3 options (dev, stg, prod)
redirectUrl: `<AUTH_REDIRECT_URI>`, // It is used just for 3 legged auth
});
Javascript instance
If you are using CommonJs modules you can use createAuthSDK
function from the module.
const authSDK = require("@adsk-sdk/auth");
const auth = authSDK.createAuthSDK({
clientId: `<AUTH_CLIENT_ID>`,
clientSecret: `<AUTH_CLIENT_ID>`,
env: `<AUTH_ENV_APP>`, // Just 3 options (dev, stg, prod)
redirectUrl: `<AUTH_REDIRECT_URI>`, // It is used just for 3 legged auth
});
There are the parameters that you can use to initialize the SDK:
| Name | Parameter | Description |
| ---------------------- | -------------- | ----------- |
| clientId
| Client ID | Your Client ID provided by your app. |
| clientSecret
| Client Secret | Your Client Secret provided by your app. |
| env
| Environemnt | It is the environment where you create your app ( dev
, stg
or prod
). |
| redirectUrl
| Redirect URL | Must match the redirect_uri parameter used in GET authorize. (3 Legged)|
Two-Legged token
If you will use 2Legged auth type, you need to call the getAccessToken
method, with an object as parameter that contain the scope to request the token.
import { AuthSDK } from '@adsk-sdk/auth';
const auth = new AuthSDK({
clientId: `<AUTH_CLIENT_ID>`,
clientSecret: `<AUTH_CLIENT_ID>`,
env: `<AUTH_ENV_APP>`, // Just 3 options (dev, stg, prod)
});
// async / await style
const token = await auth.getTwoLeggedToken({
scope: ["data:read", "data:write"]
});
if you are using then/catch promise style:
// then / catch style
auth
.getTwoLeggedToken({
scope: ["data:read", "data:write"]
})
.then((accessToken) => {
const token = accessToken;
})
.catch((error) => {
throw error;
});
Three-Legged token
In this case, you need to add one aditional parameter in your intance, redirectUrl
. This parameter is the URI where you will receive the authorization code, then you need to provide that token and the scope that you call the URL
import { AuthSDK } from '@adsk-sdk/auth';
const auth = new AuthSDK({
clientId: `<AUTH_CLIENT_ID>`,
clientSecret: `<AUTH_CLIENT_ID>`,
env: `<AUTH_ENV_APP>`, // Just 3 options (dev, stg, prod)
redirectUrl: `<AUTH_REDIRECT_URI>`, // URL to receive the auth code
});
If you will use 3Legged you have to check these 3 steps for this flow:
- Request the authentication URL with your scope with the method
loginWithRedirect
( If you don't request the URL, you will get an error ) - Go to that URL, where the Auth API will send you a verification code
- Call the method
getThreeLeggedToken
with the code value as paramerter and the scope - Receive the token from SDK
Request the authentication URL
const authorizationUrl = auth.loginWithRedirect(["data:read"]);
That method will provide you a url that you need to visit to get the autorization code as parameter
Go to that URL
After visit the url you will receive an authorization code by URL param that you will need to use when you call getThreeLeggedToken
Request the access token
After receive the authorization code, just call getThreeLeggedToken
method and receive your access token from SDK
const token = auth.getThreeLeggedToken({
callbackURL: 'http://calbackURl:3000?code=AGHdsfuisudfewNVj203',
scope: ["data:read"]
});
Refresh token
Three legged token type has a mechanism to refresh the token when is expired, you can call this method when your token is expired and it will generate a new Token
const token = auth.refreshToken();
Handling Errors
The SDK provides you a Class error that it allows you to catch the errors from SDK based on error codes. Here is a list of error codes that you can use to handle errors in you application:
Error code | Description |
---|---|
implementation_error | Shows an error when some method is called but is not implemented for that type token (2 Legged or 3 Legged). |
validation_error | Some value inside the initialization is wrong. |
server_error | Shows an error from auth API server. |
invalid_credentials | The client credentials are invalid. |
invalid_grant | The redirect_uri , scope or code are invalid. |
invalid_request | The token request must specify a valid grant_type . |
invalid_request | The request is missing a required parameter code (3 Legged). |
invalid_request | The request is missing a required parameter redirect_uri (3 Legged). |
Support + Feedback
We appreciate feedback and contribution to this repo! For support or to provide feedback, please reach out to channel on slack.