Skip to content

Commit 6ce241f

Browse files
fix(provider): change order for constructor arguments in saml (#178)
* fix(provider): changed order for constructor arguments in saml changed order for constructor arguments in saml GH-177 * fix(provider): update readme file update readme file GH-177
1 parent 34971e9 commit 6ce241f

File tree

2 files changed

+47
-29
lines changed

2 files changed

+47
-29
lines changed

README.md

Lines changed: 45 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
</a>
2121
</p>
2222

23-
2423
## Overview
2524

2625
This is a loopback-next extension for adding authentication layer to a REST application in loopback 4.
@@ -2784,15 +2783,19 @@ After this, you can use decorator to apply auth to controller functions wherever
27842783
@authenticate(
27852784
STRATEGY.SAML,
27862785
{
2787-
accessType: 'offline',
2788-
scope: ['profile', 'email'],
2789-
authorizationURL: process.env.SAML_URL,
2790-
callbackURL: process.env.SAML_CALLBACK_URL,
2791-
clientID: process.env.SAML_CLIENT_ID,
2792-
clientSecret: process.env.SAML_CLIENT_SECRET,
2793-
tokenURL: process.env.SAML_TOKEN_URL,
2794-
},
2795-
queryGen('body'),
2786+
accessType: 'offline',
2787+
scope: ['profile', 'email'],
2788+
callbackURL: process.env.SAML_CALLBACK_URL,
2789+
issuer: process.env.SAML_ISSUER,
2790+
cert: process.env.SAML_CERT,
2791+
entryPoint: process.env.SAML_ENTRY_POINT,
2792+
audience: process.env.SAML_AUDIENCE,
2793+
logoutUrl: process.env.SAML_LOGOUT_URL,
2794+
passReqToCallback: !!+(process.env.SAML_AUTH_PASS_REQ_CALLBACK ?? 0),
2795+
validateInResponseTo: !!+(process.env.VALIDATE_RESPONSE ?? 1),
2796+
idpIssuer: process.env.IDP_ISSUER,
2797+
logoutCallbackUrl: process.env.SAML_LOGOUT_CALLBACK_URL,
2798+
}
27962799
)
27972800
@authorize({permissions: ['*']})
27982801
@post('/auth/saml', {
@@ -2823,21 +2826,25 @@ After this, you can use decorator to apply auth to controller functions wherever
28232826
@authenticate(
28242827
STRATEGY.SAML,
28252828
{
2826-
accessType: 'offline',
2827-
scope: ['profile', 'email'],
2828-
authorizationURL: process.env.SAML_URL,
2829-
callbackURL: process.env.SAML_CALLBACK_URL,
2830-
clientID: process.env.SAML_CLIENT_ID,
2831-
clientSecret: process.env.SAML_CLIENT_SECRET,
2832-
tokenURL: process.env.SAML_TOKEN_URL,
2833-
},
2834-
queryGen('query'),
2829+
accessType: 'offline',
2830+
scope: ['profile', 'email'],
2831+
callbackURL: process.env.SAML_CALLBACK_URL,
2832+
issuer: process.env.SAML_ISSUER,
2833+
cert: process.env.SAML_CERT,
2834+
entryPoint: process.env.SAML_ENTRY_POINT,
2835+
audience: process.env.SAML_AUDIENCE,
2836+
logoutUrl: process.env.SAML_LOGOUT_URL,
2837+
passReqToCallback: !!+(process.env.SAML_AUTH_PASS_REQ_CALLBACK ?? 0),
2838+
validateInResponseTo: !!+(process.env.VALIDATE_RESPONSE ?? 1),
2839+
idpIssuer: process.env.IDP_ISSUER,
2840+
logoutCallbackUrl: process.env.SAML_LOGOUT_CALLBACK_URL,
2841+
}
28352842
)
28362843
@authorize({permissions: ['*']})
2837-
@get('/auth/saml-redirect', {
2844+
@post(`/auth/saml-redirect`, {
28382845
responses: {
28392846
[STATUS_CODE.OK]: {
2840-
description: 'Saml Redirect Token Response',
2847+
description: 'Okta SAML callback',
28412848
content: {
28422849
[CONTENT_TYPE.JSON]: {
28432850
schema: {[X_TS_TYPE]: TokenResponse},
@@ -2846,15 +2853,19 @@ After this, you can use decorator to apply auth to controller functions wherever
28462853
},
28472854
},
28482855
})
2849-
async samlCallback(
2850-
@param.query.string('code') code: string, //NOSONAR
2851-
@param.query.string('state') state: string,
2852-
@param.query.string('session_state') sessionState: string, //NOSONAR
2853-
@inject(RestBindings.Http.RESPONSE) response: Response,
2856+
async oktaSamlCallback(
28542857
@inject(AuthenticationBindings.CURRENT_USER)
28552858
user: AuthUser | undefined,
2859+
@inject(RestBindings.Http.REQUEST) request: Request,
2860+
@param.query.string('client') clientId: string,
2861+
@inject(RestBindings.Http.RESPONSE) response: Response,
2862+
@requestBody({
2863+
content: {
2864+
[CONTENT_TYPE.FORM_URLENCODED]: {},
2865+
},
2866+
})
2867+
oktaData: AnyObject,
28562868
): Promise<void> {
2857-
const clientId = new URLSearchParams(state).get('client_id');
28582869
if (!clientId || !user) {
28592870
throw new HttpErrors.Unauthorized(AuthErrorKeys.ClientInvalid);
28602871
}
@@ -2878,6 +2889,13 @@ After this, you can use decorator to apply auth to controller functions wherever
28782889

28792890
Please note above that we are creating two new APIs for SAML. The first one is for UI clients to hit. We are authenticating client as well, then passing the details to the SAML. Then, the actual authentication is done by SAML authorization url, which redirects to the second API we created after success. The first API method body is empty as we do not need to handle its response. The SAML provider in this package will do the redirection for you automatically.
28802891

2892+
Note: For `auth/saml-redirect` one needs to configure the SSO path by incorporating the client ID as a query parameter in existing application set up within your Okta environment for which you intend to enable SSO as follows:
2893+
2894+
```
2895+
http://localhost:3000/auth/saml-redirect?client=YOUR_CLIENT_ID
2896+
2897+
```
2898+
28812899
For accessing the authenticated AuthUser model reference, you can inject the CURRENT_USER provider, provided by the extension, which is populated by the auth action sequence above.
28822900

28832901
```ts

src/strategies/SAML/saml-strategy-factory-provider.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,14 +55,13 @@ export class SamlStrategyFactoryProvider
5555
if (options && options.passReqToCallback === true) {
5656
strategy = new Strategy(
5757
options,
58-
logoutVerify as VerifyWithRequest,
5958
// eslint-disable-next-line @typescript-eslint/no-misused-promises
6059
func,
60+
logoutVerify as VerifyWithRequest,
6161
);
6262
} else {
6363
strategy = new Strategy(
6464
options,
65-
logoutVerify as unknown as VerifyWithoutRequest,
6665
// eslint-disable-next-line @typescript-eslint/no-misused-promises
6766
async (profile: Profile | null | undefined, cb: VerifiedCallback) => {
6867
try {
@@ -77,6 +76,7 @@ export class SamlStrategyFactoryProvider
7776
cb(err);
7877
}
7978
},
79+
logoutVerify as unknown as VerifyWithoutRequest,
8080
);
8181
}
8282
this._setupProxy(strategy);

0 commit comments

Comments
 (0)