Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
export function userSessionFromAuthResult(authResult) {
return UserSession.fromOIDC({
oidcProvider: process.env.OIDC_PROVIDER,
accessToken: authResult.accessToken,
fullName: authResult.idTokenPayload.nickname,
picture: authResult.idTokenPayload.picture,
oidcSubject: authResult.idTokenPayload.sub,
// per https://wiki.mozilla.org/Security/Guidelines/OpenID_connect#Session_handling
renewAfter: fromNow('15 minutes')
});
}
async loadSecret(props) {
// If there is a secretId, we load it. Otherwise we create a new secret
if (!props.secretId) {
const defaultSecret = { foo: 'bar' };
return this.setState({
secret: defaultSecret,
secretValue: safeDump(defaultSecret, safeDumpOpts),
expires: fromNow('1000 years'),
editing: true,
loading: false,
error: null
});
}
// indicate loading while the async secret fetch occurs
this.setState({
loading: true,
error: null
});
try {
const { secret, expires } = await props.secrets.get(props.secretId);
this.setState({
//
// For more details, see:
// https://auth0.com/docs/libraries/auth0js/v9#extract-the-authresult-and-get-user-info
//
const userSession = {
idToken: authResult.idToken,
accessToken: authResult.accessToken,
fullName: authResult.idTokenPayload.nickname,
picture: authResult.idTokenPayload.picture,
oidcSubject: authResult.idTokenPayload.sub,
url: authResult.url,
// `accessTokenexpiresAt` is the unix timestamp (in seconds) at which the access token expires.
// It is used by the Django backend along with idToken's `exp` to determine session expiry.
accessTokenExpiresAt: authResult.expiresIn + Math.floor(Date.now() / 1000),
// per https://wiki.mozilla.org/Security/Guidelines/OpenID_connect#Session_handling
renewAfter: fromNow('15 minutes'),
};
return userSession;
};
this.setState({ loading: true }, async () => {
try {
const description =
this.state.query.description ||
`Client created ${new Date()} for ${this.state.query.callback_url}`;
const requestedScopes = toArray(this.state.query.scope);
const currentScopes = (await this.props.auth.currentScopes()).scopes;
await this.props.auth.updateClient(this.state.client.clientId, {
description,
expires: fromNow(this.state.query.expires || '3 days'),
scopes: scopeIntersection(currentScopes, requestedScopes),
deleteOnExpiration: true
});
const client = await this.props.auth.resetAccessToken(
this.state.client.clientId
);
this.triggerCallback(client.clientId, client.accessToken);
} catch (error) {
this.setState({ error, loading: false });
}
});
};