# jamalta-oidc-client

> JA Malta OIDC Client for external/internal login-as implementation

Latest version **0.9.91** (published 2022-06-02) · 0 weekly downloads

## Install

```sh
npm install jamalta-oidc-client
pnpm add jamalta-oidc-client
yarn add jamalta-oidc-client
bun add jamalta-oidc-client
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities.

Warnings: low downloads; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.9.91 |
| Published | 2022-06-02 |
| First published | 2022-05-26 |
| Weekly downloads | 0 |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 4 |
| Unpacked size | 96.2 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Jamie Stivala |
| Maintainers | jamiestivala |
| Keywords | ja, jamalta, oauth, openid, framework, authentication, openid-relay |

## Links

- npm: https://www.npmjs.com/package/jamalta-oidc-client
- Repository: https://github.com/jamaltaorg/JA-Malta-OIDC-Client
- Homepage: https://jamaltaorg.github.io/JA-Malta-OIDC-Client/
- Issues: https://github.com/jamaltaorg/JA-Malta-OIDC-Client/issues
- npm.io page: https://npm.io/package/jamalta-oidc-client

## Dependencies (4)

- [dotenv](https://npm.io/package/dotenv.md) ^16.0.1
- [express](https://npm.io/package/express.md) ^4.18.1
- [typedoc](https://npm.io/package/typedoc.md) ^0.22.17
- [openid-client](https://npm.io/package/openid-client.md) ^5.1.6

## Alternatives

- [@pothos/plugin-authz](https://npm.io/package/@pothos/plugin-authz.md) — 12.4K weekly downloads
- [@bounded-sh/client](https://npm.io/package/@bounded-sh/client.md) — 3.2K weekly downloads
- [@luigi-project/plugin-auth-oauth2](https://npm.io/package/@luigi-project/plugin-auth-oauth2.md) — 2.3K weekly downloads
- [@nocobase/plugin-verification](https://npm.io/package/@nocobase/plugin-verification.md) — 2.0K weekly downloads
- [escher-request](https://npm.io/package/escher-request.md) — 545 weekly downloads

## Recent versions

- 0.9.91 (latest) — 2022-06-02
- 0.9.9 — 2022-06-02
- 0.9.8 — 2022-06-02
- 0.9.7 — 2022-06-02
- 0.9.6 — 2022-06-02
- 0.9.4 — 2022-05-26
- 0.9.3 — 2022-05-26

## README

# JA Malta OIDC Relying Party Module
[![Docs](https://img.shields.io/github/workflow/status/jamaltaorg/JA-Malta-OIDC-Client/Release%20&%20Publish?label=Documentation&logo=github)](https://jamaltaorg.github.io/JA-Malta-OIDC-Client/) [![Release & Publish](https://github.com/jamaltaorg/JA-Malta-OIDC-Client/actions/workflows/publish.yml/badge.svg)](https://github.com/jamaltaorg/JA-Malta-OIDC-Client/actions/workflows/publish.yml) [![npm](https://img.shields.io/npm/v/jamalta-oidc-client)](https://www.npmjs.com/package/jamalta-oidc-client)

This module is meant to be used when needing authentication with the JA Malta Platforms.  The authentication, ran under the URI auth.jamalta.org, uses OpenID Connect specs and [discovery url](https://auth.jamalta.org/.well-known/openid-configuration). 

**Please note that the authentication and this connector is still in the early alpha testing.  Currently, this module is meant to be used internally until everything is tested**.

This package can technically be used by other OIDC Providers but **this was not tested and no support will be provided**.  

Feel free to open a PR and add/suggest features (no contribution guide yet).
## Features
The connector has the following available features:
1. Express Middleware to authenticate a token including a cache that lasts as long as the access-token.
2. Session continuation after login is hit when token expiry
3. UserInfo cache, to not constantly call the authentication endpoint everytime user info is needed.
4. OpenID Discovery Specs

#### Assumptions
Cookie Middleware (such as cookie parser).
ExpressJS being used

## Getting Started
Getting started is pretty easy.  

### Installing the module
```bash
npm install jamalta-oidc-client
```

#### Creating the issuer
```js 
let issuer = new JAMaltaIssuer(clientId, clientSecret, redirectUris, responseTypes, scopes);
```

#### Getting the login url
Once the issuer is created, one can get the authorisation url by doing:
```js
let url = await issuer.authorisationUrl;
```

#### Login Callback
From there onwards, create an express route with the callback and **implement the callback middleware**.  The callback middleware creates a cookie on the frontend which can be read by the authentication middleware.  The callback middleware also gets the cookie "before-callback-location", which stores the original location to redirect the user to their original location.  This feature can be disabled by setting the useRedirect parameter to false.
```js
expressApp.get("/callback", callbackMiddleware(issuer, true), (req, res) => {
    //your code here
    res.sendStatus(200);
});
```

#### Authentication Middleware
After initial authentication is done, the issuer does everything for you using the authentication middleware.  In the case that the token is invalid, the user gets redirected to the login page and a cookie is created called "before-callback-location" so the backend knows where to redirect the user to.
```js
expressApp.get("/your-endpoint", authenticate(issuer), (req, res) => {
    let userinfo = req.jaUserInfo;
    res.sendStatus(200);
})
```
The user info is then stored into the request as seen above, and can be used in any way needed.

#### Logout
The issuer also provides the functionality to logout easily by doing `let url = await issuer.getLogoutUrl(token);`  The logout middleware is used to clear the token, and it's related user information from the cache, however, this is optional, **considering you do the steps yourself**.  Not clearing the cache will result in the user seemingly staying authenticated until cache expires.
```js
expressApp.get("/logout", authenticate(issuer), logoutMiddleware(issuer), (req, res) => {
   issuer.getLogoutUrl(req.tokenCode).then(value => res.redirect(value));
})
```

## Developer Features
### Custom Issuer URL
In the case that you are developing onto the OIDC Relay, and want to use a custom issuer URL, you can set the environment variable `CUSTOM_ISSUER_URL` which forces auto-discovery and endpoint binding to that OIDC Provider, **assuming OIDC discovery is enabled on the OIDC Provider**.

---
_Source: https://npm.io/package/jamalta-oidc-client · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
