6.25.6 • Published 1 year ago

@axa-fr/vanilla-oidc v6.25.6

Weekly downloads
169
License
MIT
Repository
github
Last release
1 year ago

@axa-fr/vanilla-oidc

Continuous Integration Quality Gate Reliability Security Code Corevage Twitter

Try the demo at https://icy-glacier-004ab4303.2.azurestaticapps.net/

About

@axa-fr/vanilla-oidc is a pure OIDC client library agnostic to any framework. It is used by @axa-fr/react-oidc and can be used by any framework.

It is a real alternative to existing oidc-client libraries.

  • Secure :
    • With the use of Service Worker, your tokens (refresh_token and access_token) are not accessible to the JavaScript client code (big protection against XSRF attacks)
    • OIDC using client side Code Credential Grant with PKCE only
  • Lightweight
  • Simple :
    • refresh_token and access_token are auto refreshed in background
    • with the use of the Service Worker, you do not need to inject the access_token in every fetch, you have only to configure OidcTrustedDomains.js file
  • No cookies problem : You can disable silent signin (that internally use an iframe). For your information, your OIDC server should be in the same domain of your website in order to be able to send OIDC server cookies from your website via an internal IFRAME, else, you may encounter COOKIES problem.
  • Multiple Authentication :
    • You can authenticate many times to the same provider with different scope (for example you can acquire a new 'payment' scope for a payment)
    • You can authenticate to multiple different providers inside the same SPA (single page application) website
  • Flexible :
    • Work with Service Worker (more secure) and without for older browser (less secure)

npm.io

The service worker catch access_token and refresh_token that will never be accessible to the client.

Getting Started

npm install @axa-fr/vanilla-oidc --save

# If you have a "public" folder, the 2 files will be created :
# ./public/OidcServiceWorker.js <-- will be updated at each "npm install"
# ./public/OidcTrustedDomains.js <-- won't be updated if already exist

If you need a very secure mode where refresh_token and access_token will be hide behind a service worker that will proxify requests. The only file you should edit is "OidcTrustedDomains.js".

// OidcTrustedDomains.js

// Add bellow trusted domains, access tokens will automatically injected to be send to
// trusted domain can also be a path like https://www.myapi.com/users,
// then all subroute like https://www.myapi.com/useers/1 will be authorized to send access_token to.

// Domains used by OIDC server must be also declared here
const trustedDomains = {
  default: ["https://demo.duendesoftware.com", "https://www.myapi.com/users"],
};

The code of the demo :

import { VanillaOidc } from '@axa-fr/vanilla-oidc'

export const configuration = {
    client_id: 'interactive.public.short',
    redirect_uri: window.location.origin + '/#/authentication/callback',
    silent_redirect_uri: window.location.origin + '/#/authentication/silent-callback',
    scope: 'openid profile email api offline_access',
    authority: 'https://demo.duendesoftware.com',
    service_worker_relative_url:'/OidcServiceWorker.js',
    service_worker_only: false,
};

const href = window.location.href;
const vanillaOidc = VanillaOidc.getOrCreate(() => fetch)(configuration);

console.log(href);

vanillaOidc.tryKeepExistingSessionAsync().then(() => {
    if(href.includes(configuration.redirect_uri)){
        vanillaOidc.loginCallbackAsync().then(()=>{
            window.location.href = "/";
        });
        document.body.innerHTML = `<div>
            <h1>@axa-fr/vanilla-oidc demo</h1>
            <h2>Loading</h2>
        </div>`;
        return
    }

    let tokens = vanillaOidc.tokens;

    if(tokens){

        // @ts-ignore
        window.logout = () =>  vanillaOidc.logoutAsync();
        document.body.innerHTML = `<div>
            <h1>@axa-fr/vanilla-oidc demo</h1>
            <button onclick="window.logout()">Logout</button>
            <h2>Authenticated</h2>
            <pre>${JSON.stringify(tokens,null,'\t')}</pre>
        </div>`
        
    }
    else {
        // @ts-ignore
        window.login= () =>  vanillaOidc.loginAsync("/");
        document.body.innerHTML = `<div>
            <h1>@axa-fr/vanilla-oidc demo</h1>
            <button onclick="window.login()">Login</button>
        </div>`
    }
})

Configuration

const configuration: {
    client_id: PropTypes.string.isRequired, // oidc client id
    redirect_uri: PropTypes.string.isRequired, // oidc redirect url
    silent_redirect_uri: PropTypes.string, // Optional activate silent-signin that use cookies between OIDC server and client javascript to restore sessions
    silent_login_uri: PropTypes.string, // Optional, route that trigger the signin
    silent_login_timeout: PropTypes.number, // Optional default is 12000 milliseconds
    scope: PropTypes.string.isRequired, // oidc scope (you need to set "offline_access")
    authority: PropTypes.string.isRequired,
    storage: Storage, // Default sessionStorage, you can set localStorage but it is less secure to XSS attacks
    authority_configuration: PropTypes.shape({
      // Optional for providers that does not implement OIDC server auto discovery via a .wellknowurl
      authorization_endpoint: PropTypes.string,
      token_endpoint: PropTypes.string,
      userinfo_endpoint: PropTypes.string,
      end_session_endpoint: PropTypes.string,
      revocation_endpoint: PropTypes.string,
      check_session_iframe: PropTypes.string,
      issuer: PropTypes.string,
    }),
    refresh_time_before_tokens_expiration_in_second: PropTypes.number, // default is 120 seconds
    service_worker_relative_url: PropTypes.string,
    service_worker_only: PropTypes.boolean, // default false
    service_worker_convert_all_requests_to_cors: PropTypes.boolean, // force all requests that servie worker upgrades to have 'cors' mode. This allows setting authentication token on requests initialted by html parsing(e.g. img tags, download links etc).
    extras: StringMap | undefined, // ex: {'prompt': 'consent', 'access_type': 'offline'} list of key/value that are send to the oidc server (more info: https://github.com/openid/AppAuth-JS)
    token_request_extras: StringMap | undefined, // ex: {'prompt': 'consent', 'access_type': 'offline'} list of key/value that are send to the oidc server during token request (more info: https://github.com/openid/AppAuth-JS)
    withCustomHistory: PropTypes.function, // Override history modification, return instance with replaceState(url, stateHistory) implemented (like History.replaceState())
    authority_time_cache_wellknowurl_in_second: 60 * 60, // Time to cache in second of openid wellknowurl, default is 1 hour
    authority_timeout_wellknowurl_in_millisecond: 10000, // Timeout in millisecond of openid wellknowurl, default is 10 seconds, then error is throwed
    monitor_session: PropTypes.boolean, // Add OpenId monitor session, default is false (more information https://openid.net/specs/openid-connect-session-1_0.html), if you need to set it to true consider https://infi.nl/nieuws/spa-necromancy/
    onLogoutFromAnotherTab: Function, // Optional, can be set to override the default behavior, this function is triggered when user with the same subject is logged out from another tab when session_monitor is active
    onLogoutFromSameTab: Function, // Optional, can be set to override the default behavior, this function is triggered when user is logged out from same tab when session_monitor is active
    token_renew_mode: PropTypes.string, // Optional, update tokens base on the selected token(s) lifetime: "access_token_or_id_token_invalid" (default), "access_token_invalid" , "id_token_invalid"
    logout_tokens_to_invalidate : Array<string> // Optional tokens to invalidate during logout, default: ['access_token', 'refresh_token']
  };

Run The Demo

git clone https://github.com/AxaGuilDEv/react-oidc.git
cd react-oidc/packages/vanilla-demo
npm install
npm start
# then navigate to http://localhost:3000

How It Works

This component is a pure vanilla JS OIDC client library agnostic to any framework. It is a real alternative to existing oidc-client libraries.

More information about OIDC

Hash route

vanilla-oidc work also with hash route.

export const configurationIdentityServerWithHash = {
  client_id: "interactive.public.short",
  redirect_uri: window.location.origin + "#authentication-callback",
  silent_redirect_uri:
    window.location.origin + "#authentication-silent-callback",
  scope: "openid profile email api offline_access",
  authority: "https://demo.duendesoftware.com",
  refresh_time_before_tokens_expiration_in_second: 70,
  service_worker_relative_url: "/OidcServiceWorker.js",
  service_worker_only: false,
};

Service Worker Support

  • Firefox : tested on Firefox 98.0.2
  • Chrome/Edge : tested on version upper to 90
  • Opera : tested on version upper to 80
  • Safari : tested on Safari/605.1.15
6.24.27-beta929

1 year ago

6.25.2-alpha949

1 year ago

6.24.0

1 year ago

6.24.1

1 year ago

6.25.0-alpha939

1 year ago

6.25.5

1 year ago

6.25.6

1 year ago

6.25.4

1 year ago

6.25.4-alpha952

1 year ago

6.23.1

1 year ago

6.23.2

1 year ago

6.25.1-alpha941

1 year ago

6.25.3-alpha950

1 year ago

6.20.0

1 year ago

6.22.14

1 year ago

6.22.15

1 year ago

6.22.16

1 year ago

6.22.11

1 year ago

6.23.0

1 year ago

6.22.9

1 year ago

6.22.7

1 year ago

6.17.0

2 years ago

6.17.1

2 years ago

6.16.14

2 years ago

6.16.13

2 years ago

6.19.0

2 years ago

6.19.1

1 year ago

6.19.2

1 year ago

6.18.1

2 years ago

6.18.2

2 years ago

6.18.0

2 years ago

6.18.3

2 years ago

6.14.12

2 years ago

6.14.11

2 years ago

6.14.10

2 years ago

6.16.9

2 years ago

6.16.1

2 years ago

6.16.2

2 years ago

6.16.7

2 years ago

6.16.8

2 years ago

6.16.6

2 years ago

6.16.0

2 years ago

6.15.5

2 years ago

6.15.4

2 years ago

6.15.3

2 years ago

6.15.2

2 years ago

6.15.9

2 years ago

6.15.8

2 years ago

6.15.7

2 years ago

6.15.6

2 years ago

6.15.1

2 years ago

6.15.0

2 years ago

6.14.6

2 years ago

6.14.5

2 years ago

6.14.4

2 years ago

6.14.3

2 years ago

6.14.9

2 years ago

6.14.8

2 years ago

6.14.7

2 years ago

6.14.2

2 years ago

6.16.10

2 years ago

6.16.12

2 years ago

6.14.1

2 years ago

6.14.0

2 years ago

6.13.4

2 years ago

6.13.3

2 years ago

6.13.2

2 years ago

6.13.1

2 years ago

6.13.0

2 years ago

6.12.0

2 years ago

6.12.1

2 years ago

6.11.3

2 years ago

6.11.2

2 years ago

6.10.9-alpha0

2 years ago

6.10.9-alpha1

2 years ago

6.10.21

2 years ago

6.10.20

2 years ago

6.10.11

2 years ago

6.8.2

2 years ago

6.10.19

2 years ago

6.10.18

2 years ago

6.10.17

2 years ago

6.10.16

2 years ago

6.10.15

2 years ago

6.10.14

2 years ago

6.10.13

2 years ago

6.10.12

2 years ago

6.9.0

2 years ago

6.9.2

2 years ago

6.9.1

2 years ago

6.9.4

2 years ago

6.9.3

2 years ago

6.9.6

2 years ago

6.9.5

2 years ago

6.9.7

2 years ago

6.11.1

2 years ago

6.11.0

2 years ago

6.10.9

2 years ago

6.10.8

2 years ago

6.10.7

2 years ago

6.10.2

2 years ago

6.10.1

2 years ago

6.10.0

2 years ago

6.10.6

2 years ago

6.10.5

2 years ago

6.10.4

2 years ago

6.10.3

2 years ago

6.6.7

2 years ago

6.6.8

2 years ago

6.7.0

2 years ago

6.8.1

2 years ago

6.8.0

2 years ago

6.5.10

2 years ago

6.5.0

2 years ago

6.6.1

2 years ago

6.6.0

2 years ago

6.6.3

2 years ago

6.6.2

2 years ago

6.6.5

2 years ago

6.6.4

2 years ago

6.6.6

2 years ago

6.0.13

2 years ago

6.1.0

2 years ago

6.2.0

2 years ago

6.3.0

2 years ago

6.3.2

2 years ago

6.3.1

2 years ago

6.4.1

2 years ago

6.4.0

2 years ago

6.5.2

2 years ago

6.5.1

2 years ago

6.5.4

2 years ago

6.5.3

2 years ago

6.5.6

2 years ago

6.5.5

2 years ago

6.5.8

2 years ago

6.5.7

2 years ago

6.5.9

2 years ago

5.11.1

2 years ago

5.11.0

2 years ago

5.8.1

2 years ago

5.8.0

2 years ago

5.12.0

2 years ago

5.13.9

2 years ago

5.13.8

2 years ago

5.13.7

2 years ago

6.0.12

2 years ago

5.13.6

2 years ago

6.0.11

2 years ago

5.13.5

2 years ago

6.0.10

2 years ago

5.13.4

2 years ago

5.13.3

2 years ago

5.13.12

2 years ago

5.9.0

2 years ago

5.13.10

2 years ago

5.13.11

2 years ago

5.13.2

2 years ago

5.13.1

2 years ago

5.13.0

2 years ago

5.10.9

2 years ago

5.10.8

2 years ago

5.10.7

2 years ago

5.14.2

2 years ago

5.10.6

2 years ago

6.0.7

2 years ago

6.0.6

2 years ago

6.0.9

2 years ago

6.0.8

2 years ago

6.0.1

2 years ago

6.0.0

2 years ago

6.0.3

2 years ago

6.0.2

2 years ago

6.0.5

2 years ago

6.0.4

2 years ago

5.14.1

2 years ago

5.10.5

2 years ago

5.14.0

2 years ago

5.10.4

2 years ago

5.10.3

2 years ago

5.10.2

2 years ago

5.10.1

2 years ago

5.10.0

2 years ago

5.7.8

2 years ago

5.4.2

3 years ago

5.4.1

3 years ago

5.5.1

3 years ago

5.5.0

3 years ago

5.6.0

3 years ago

5.7.7

3 years ago

5.7.6

3 years ago

5.7.5

3 years ago

5.7.4

3 years ago

5.7.3

3 years ago

5.7.2

3 years ago

5.7.0-aplha0

3 years ago

5.3.1

3 years ago

5.3.0

3 years ago

4.5.8

3 years ago

4.5.7

3 years ago

4.5.9

3 years ago

4.5.4

3 years ago

4.5.3

3 years ago

4.5.6

3 years ago

4.5.5

3 years ago

4.2.12-alpha.0

3 years ago

4.4.1

3 years ago

5.0.9

3 years ago

5.0.8

3 years ago

5.0.7

3 years ago

5.0.6

3 years ago

5.0.5

3 years ago

5.4.0

3 years ago

5.0.4

3 years ago

5.0.3

3 years ago

5.0.2

3 years ago

5.0.1

3 years ago

5.0.0

3 years ago

4.5.10

3 years ago

4.5.11

3 years ago

4.2.11-alpha.0

3 years ago

4.5.12

3 years ago

4.5.13

3 years ago

4.2.11-alpha.1

3 years ago

4.3.2

3 years ago

4.3.1

3 years ago

4.2.10

3 years ago

4.2.11

3 years ago

4.3.3

3 years ago

4.2.12

3 years ago

4.3.0

3 years ago

4.2.13

3 years ago

5.1.3

3 years ago

5.1.2

3 years ago

5.1.1

3 years ago

4.4.0-alpha.0

3 years ago

4.2.14-alpha.0

3 years ago

5.2.2

3 years ago

5.2.1

3 years ago

5.2.0

3 years ago

4.5.0

3 years ago

4.5.2

3 years ago

4.5.1

3 years ago

4.2.3

3 years ago

4.2.2

3 years ago

4.2.5

3 years ago

4.2.4

3 years ago

4.2.1

3 years ago

4.0.3

3 years ago

4.2.0

3 years ago

4.0.2

3 years ago

4.2.7

3 years ago

4.2.6

3 years ago

4.2.9

3 years ago

4.2.8

3 years ago

4.2.3-alpha.0

3 years ago

4.2.9-alpha.0

3 years ago

4.0.0-alpha.9

3 years ago

4.0.0-alpha.17

3 years ago

4.0.0-alpha.16

3 years ago

4.0.0-alpha.15

3 years ago

4.0.0-alpha.14

3 years ago

4.0.0-alpha.19

3 years ago

4.0.0-alpha.13

3 years ago

4.0.0-alpha.10

3 years ago

4.0.1

3 years ago

4.0.0

3 years ago

4.0.0-beta.8

3 years ago

4.0.0-beta.7

3 years ago

4.0.0-beta.4

3 years ago

4.0.0-beta.3

3 years ago

4.0.0-beta.2

3 years ago

4.0.0-beta.1

3 years ago

4.0.0-alpha.20

3 years ago

4.0.0-alpha.23

3 years ago

4.0.0-alpha.22

3 years ago

4.0.0-beta.9

3 years ago

4.0.0-alpha.21

3 years ago

3.1.7

3 years ago

3.1.6

4 years ago

3.1.5

4 years ago

3.1.4

4 years ago

3.1.3

4 years ago

3.1.2

4 years ago

3.1.1

4 years ago

3.1.1-alpha.0

4 years ago

3.1.0-alpha.0

5 years ago

3.0.9-alpha.0

5 years ago

3.0.8

5 years ago

3.0.8-alpha.0

5 years ago

3.0.7

5 years ago

3.0.6

5 years ago

3.0.6-alpha.0

5 years ago

3.0.5-alpha.0

5 years ago

3.0.4-alpha.0

5 years ago

3.0.3-alpha.0

5 years ago

3.0.2-alpha.0

5 years ago

3.0.1-alpha.0

5 years ago

3.0.0-alpha.0

5 years ago

2.0.8

5 years ago

2.0.7

5 years ago

2.0.6

5 years ago

2.0.5

5 years ago

2.0.4

5 years ago

2.0.3

5 years ago

2.0.2

5 years ago

2.0.2-alpha.0

5 years ago

2.0.0-alpha.0

5 years ago

1.3.3

6 years ago

1.3.3-alpha.0

6 years ago

1.3.2-alpha.0

6 years ago

1.3.1-alpha.0

6 years ago

1.3.0

6 years ago

1.2.1

6 years ago