From 585b1404f6af4ad397f5f7acf24c57b0f28a59f5 Mon Sep 17 00:00:00 2001 From: Abdulghani Akhras Date: Thu, 15 Dec 2022 14:16:58 +0000 Subject: [PATCH] feat(add typescript support): - Update Dependencies --- README.md | 2 +- lib/strategies/webapp-strategy.js | 2 + lib/types/appid-sdk.d.ts | 70 +++++++++++++++++++ lib/types/appid-sdk.test-d.ts | 54 ++++++++++++++ .../user-profile-manager.js | 2 +- package.json | 6 +- samples/cloud-directory-app-sample-server.js | 9 +-- samples/web-app-sample-server.js | 7 +- 8 files changed, 141 insertions(+), 11 deletions(-) create mode 100644 lib/types/appid-sdk.d.ts create mode 100644 lib/types/appid-sdk.test-d.ts diff --git a/README.md b/README.md index a9d30025..fa6986bd 100644 --- a/README.md +++ b/README.md @@ -458,7 +458,7 @@ Note: ### Self Service APIs -Use the self service manager when you want to control the UI for the sign-in, sign-up, forgot password, changeDetail and change password flows. +Use the self service manager when you want to control the UI for the sign-up, forgot password, changeDetail and change password flows. The selfServiceManager can be init with the following options: * iamApiKey: If supplied, it will be used to get iamToken before every request of the selfServiceManager. diff --git a/lib/strategies/webapp-strategy.js b/lib/strategies/webapp-strategy.js index 31ab511f..c1490e5a 100644 --- a/lib/strategies/webapp-strategy.js +++ b/lib/strategies/webapp-strategy.js @@ -146,6 +146,7 @@ WebAppStrategy.prototype.authenticate = function (req, options = {}) { return this.redirect(url); } else { // Handle authorization request + options.keepSessionInfo = true; return handleAuthorization(req, options, this); } }; @@ -324,6 +325,7 @@ function handleCallback(req, options = {}, strategy) { logger.debug("handleCallback"); options.failureRedirect = options.failureRedirect || "/"; options.successReturnToOrRedirect = "/"; // fallback to req.session.returnTo + options.keepSessionInfo = true; const stateParameter = req.session[WebAppStrategy.STATE_PARAMETER]; // Check for handleChangeDetails, handleForgotPassword callback from cloud directory const cloudDirectoryUpdate = req.session[WebAppStrategy.CLOUD_DIRECTORY_UPDATE_REQ]; diff --git a/lib/types/appid-sdk.d.ts b/lib/types/appid-sdk.d.ts new file mode 100644 index 00000000..80d32e36 --- /dev/null +++ b/lib/types/appid-sdk.d.ts @@ -0,0 +1,70 @@ +// Author : Younes A + +export interface StrategyOptions { + [key: string]: any; +} + +export interface SelfServiceOptions { + iamApiKey?: string; + managementUrl?: string; + tenantId?: string; + oAuthServerUrl?: string; + iamTokenUrl?: string; +} + +export class Strategy { + authenticate: () => void; +} + +export interface ApplicationIdentityToken { + accessToken: string; + tokenType: string; + expiresIn: number; +} + +export interface CustomIdentityToken extends ApplicationIdentityToken { + identityToken: string; +} + +export interface UserSCIM { + id: string; + userName: string; + [key: string]: any; +} + +// tslint:disable-next-line:no-unnecessary-class +export class APIStrategy extends Strategy { + constructor(options: StrategyOptions); +} + +// tslint:disable-next-line:no-unnecessary-class +export class WebAppStrategy extends Strategy { + constructor(options: StrategyOptions); +} + +// tslint:disable-next-line:no-unnecessary-class +export class TokenManager { + constructor(options: StrategyOptions); + getApplicationIdentityToken: () => Promise; + getCustomIdentityTokens: () => Promise; +} + +// tslint:disable-next-line:no-unnecessary-class +export class SelfServiceManager { + constructor(options: SelfServiceOptions); + signUp: ( + userData: unknown, + language: string, + iamToken: string + ) => Promise; +} + +// tslint:disable-next-line:no-unnecessary-class +export class UserProfileManager { + constructor(); +} + +// tslint:disable-next-line:no-unnecessary-class +export class UnauthorizedException { + constructor(); +} \ No newline at end of file diff --git a/lib/types/appid-sdk.test-d.ts b/lib/types/appid-sdk.test-d.ts new file mode 100644 index 00000000..7392aee5 --- /dev/null +++ b/lib/types/appid-sdk.test-d.ts @@ -0,0 +1,54 @@ +// Author : Younes A +import { + APIStrategy, + ApplicationIdentityToken, + SelfServiceManager, + Strategy, + TokenManager, + UserSCIM, + WebAppStrategy, +} from "./appid-sdk"; +import { expectType } from "tsd"; + +expectType( + new APIStrategy({ + oauthServerUrl: "{oauth-server-url}", + }) +); + +expectType( + new WebAppStrategy({ + tenantId: "{tenant-id}", + clientId: "{client-id}", + secret: "{secret}", + oauthServerUrl: "{oauth-server-url}", + redirectUri: "{app-url}" + "CALLBACK_URL", + }) +); + +const config = { + tenantId: "{tenant-id}", + clientId: "{client-id}", + secret: "{secret}", + oauthServerUrl: "{oauth-server-url}", +}; + +const tokenManager = new TokenManager(config); +expectType( + await tokenManager.getApplicationIdentityToken() +); + +const selfServiceManager = new SelfServiceManager({ + iamApiKey: "{iam-api-key}", + managementUrl: "{management-url}", +}); + +const userData = { + id: "2819c223-7f76-453a-919d-413861904646", + externalId: "701984", + userName: "bjensen@example.com", +}; + +expectType( + await selfServiceManager.signUp(userData, "en", "iamToken") +); \ No newline at end of file diff --git a/lib/user-profile-manager/user-profile-manager.js b/lib/user-profile-manager/user-profile-manager.js index 45183b80..ae4fd9c9 100644 --- a/lib/user-profile-manager/user-profile-manager.js +++ b/lib/user-profile-manager/user-profile-manager.js @@ -35,7 +35,7 @@ function UserProfileManager() {} * Initialize user profile manager * @param {Object} options The options object initializes the object with specific settings, If you are uploading the application to IBM cloud, those credentials can be read from IBM cloud and you can initialize this object without any options * @param {string} options.appidServiceEndpoint appid's service endpoint - * @param {string} options.version appid's server version in a format if v3/v4 + * @param {string} options.version appid's server version in a number format (3 OR 4) * @param {string} options.tenantId your application tenantId * @param {string} options.oauthServerUrl appid's server url- needs to be provided if service endpoint isn't provided * @param {string} options.profilesUrl appid's user profile url - needs to be provided if service endpoint isn't provided diff --git a/package.json b/package.json index 4cefc9d2..71156be2 100644 --- a/package.json +++ b/package.json @@ -12,6 +12,7 @@ "semantic-release": "semantic-release", "acp": "git add . && npm run commit" }, + "types": "./lib/appid-sdk.d.ts", "repository": { "type": "git", "url": "git+https://github.com/ibm-cloud-security/appid-serversdk-nodejs.git" @@ -28,11 +29,11 @@ "eslint-config-airbnb-base": "^13.1.0", "eslint-plugin-import": "^2.22.0", "eslint-plugin-node": "^8.0.0", - "express": "^4.17.1", + "express": "^4.17.3", "express-session": "^1.17.1", "mocha": "^5.2.0", "nyc": "^15.0.0", - "passport": "^0.4.1", + "passport": "0.6.0", "proxyquire": "^2.1.3", "rewire": "^4.0.1", "semantic-release": "^18.0.0", @@ -50,6 +51,7 @@ "helmet": "^3.23.3", "jsonwebtoken": "^8.5.1", "log4js": "^6.4.1", + "tsd": "^0.20.0", "q": "^1.5.1", "rsa-pem-from-mod-exp": "^0.8.4", "underscore": "^1.10.2" diff --git a/samples/cloud-directory-app-sample-server.js b/samples/cloud-directory-app-sample-server.js index 7f05ce87..42d57025 100644 --- a/samples/cloud-directory-app-sample-server.js +++ b/samples/cloud-directory-app-sample-server.js @@ -132,7 +132,7 @@ app.post(ROP_SUBMIT, function(req, res, next) { req.flash('errorCode', info.code); return res.redirect(ROP_LOGIN_PAGE_URL + languageQuery + emailInputQuery); } - req.logIn(user, function (err) { + req.logIn(user, { keepSessionInfo: true }, function (err) { if (err) { return next(err); } @@ -237,9 +237,10 @@ app.post(CHANGE_DETAILS_SUBMIT, passport.authenticate(WebAppStrategy.STRATEGY_NA }); // Logout endpoint. Clears authentication information from session -app.get(LOGOUT_URL, function(req, res){ - WebAppStrategy.logout(req); - res.redirect(LANDING_PAGE_URL); +app.get(LOGOUT_URL, function (req, res){ + req.session.destroy(function (err) { + res.redirect(LANDING_PAGE_URL); + }); }); diff --git a/samples/web-app-sample-server.js b/samples/web-app-sample-server.js index 2645e96f..6babb0ec 100644 --- a/samples/web-app-sample-server.js +++ b/samples/web-app-sample-server.js @@ -134,9 +134,10 @@ app.get(LOGIN_ANON_URL, passport.authenticate(WebAppStrategy.STRATEGY_NAME, { app.get(CALLBACK_URL, passport.authenticate(WebAppStrategy.STRATEGY_NAME)); // Logout endpoint. Clears authentication information from session -app.get(LOGOUT_URL, function (req, res) { - WebAppStrategy.logout(req); - res.redirect(LANDING_PAGE_URL); +app.get(LOGOUT_URL, function (req, res){ + req.session.destroy(function (err) { + res.redirect(LANDING_PAGE_URL); + }); }); function storeRefreshTokenInCookie(req, res, next) {