Skip to content

Commit

Permalink
feat(add typescript support): - Update Dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
Abdulghani Akhras authored and Abdulghani Akhras committed Dec 15, 2022
1 parent dea84d8 commit 585b140
Show file tree
Hide file tree
Showing 8 changed files with 141 additions and 11 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 2 additions & 0 deletions lib/strategies/webapp-strategy.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
};
Expand Down Expand Up @@ -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];
Expand Down
70 changes: 70 additions & 0 deletions lib/types/appid-sdk.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// Author : Younes A <https://github.com/younes-io>

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<ApplicationIdentityToken | Error>;
getCustomIdentityTokens: () => Promise<CustomIdentityToken | Error>;
}

// tslint:disable-next-line:no-unnecessary-class
export class SelfServiceManager {
constructor(options: SelfServiceOptions);
signUp: (
userData: unknown,
language: string,
iamToken: string
) => Promise<UserSCIM>;
}

// tslint:disable-next-line:no-unnecessary-class
export class UserProfileManager {
constructor();
}

// tslint:disable-next-line:no-unnecessary-class
export class UnauthorizedException {
constructor();
}
54 changes: 54 additions & 0 deletions lib/types/appid-sdk.test-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// Author : Younes A <https://github.com/younes-io>
import {
APIStrategy,
ApplicationIdentityToken,
SelfServiceManager,
Strategy,
TokenManager,
UserSCIM,
WebAppStrategy,
} from "./appid-sdk";
import { expectType } from "tsd";

expectType<Strategy>(
new APIStrategy({
oauthServerUrl: "{oauth-server-url}",
})
);

expectType<Strategy>(
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<ApplicationIdentityToken | Error>(
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: "[email protected]",
};

expectType<UserSCIM>(
await selfServiceManager.signUp(userData, "en", "iamToken")
);
2 changes: 1 addition & 1 deletion lib/user-profile-manager/user-profile-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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",
Expand All @@ -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"
Expand Down
9 changes: 5 additions & 4 deletions samples/cloud-directory-app-sample-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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);
});
});


Expand Down
7 changes: 4 additions & 3 deletions samples/web-app-sample-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 585b140

Please sign in to comment.