Installations
npm install @dsb-norge/vue-keycloak-js
Developer Guide
Typescript
No
Module System
CommonJS, UMD
Node Version
18.16.0
NPM Version
9.6.7
Score
65.6
Supply Chain
99.1
Quality
81.8
Maintenance
100
Vulnerability
100
License
Releases
v2.4.0
Published on 05 Jun 2023
Prerelase: updated instantiation method for KC class
Published on 05 Jun 2023
v2.4.0-0
Published on 05 Jun 2023
v2.3.0
Published on 01 Jun 2023
Bumping keycloak-js to 18.0.1
Published on 24 Jan 2023
Fixed build output for 2.1.4-beta release
Published on 31 May 2022
Contributors
Unable to fetch Contributors
Languages
TypeScript (86.3%)
JavaScript (13.7%)
Developer
Download Statistics
Total Downloads
983,388
Last Day
204
Last Week
5,143
Last Month
23,607
Last Year
293,721
GitHub Statistics
273 Stars
191 Commits
99 Forks
13 Watching
1 Branches
16 Contributors
Package Meta Information
Latest Version
2.4.0
Package Id
@dsb-norge/vue-keycloak-js@2.4.0
Unpacked Size
53.02 kB
Size
9.58 kB
File Count
8
NPM Version
9.6.7
Node Version
18.16.0
Publised On
05 Jun 2023
Total Downloads
Cumulative downloads
Total Downloads
983,388
Last day
-85.5%
204
Compared to previous day
Last week
-12.3%
5,143
Compared to previous week
Last month
8.4%
23,607
Compared to previous month
Last year
20%
293,721
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
Dependencies
1
Peer Dependencies
1
vue-keycloak plugin
Introduction
This plugin uses the official Keycloak JS adapter https://github.com/keycloak/keycloak/tree/main/js/libs/keycloak-js
Please read the documentation: http://www.keycloak.org/docs/latest/securing_apps/index.html#_javascript_adapter
Excerpt from Keycloak JS doc:
By default to authenticate you need to call the login function. However, there are two options available to make the adapter automatically authenticate. You can pass login-required or check-sso to the init function. login-required will authenticate the client if the user is logged-in to Keycloak or display the login page if not. check-sso will only authenticate the client if the user is already logged-in, if the user is not logged-in the browser will be redirected back to the application and remain unauthenticated.
To enable login-required set onLoad to login-required and pass to the init method:
keycloak.init({ onLoad: 'login-required' })
Installation
Install using npm
npm install @dsb-norge/vue-keycloak-js --save
Install using yarn
yarn add @dsb-norge/vue-keycloak-js
Usage
Vue 2
Vue.use(VueKeyCloak, [options])
Tell Vue to install the plugin, and optionally pass in a JavaScript object for additional configuration.
1import VueKeyCloak from '@dsb-norge/vue-keycloak-js' 2 3Vue.use(VueKeyCloak) 4 5// You can also pass in options. Check options reference below. 6Vue.use(VueKeyCloak, options)
Vue 3
createApp(App).use(VueKeycloak, [options])
Tell Vue to install the plugin, and optionally pass in a JavaScript object for additional configuration.
1import VueKeyCloak from '@dsb-norge/vue-keycloak-js' 2 3createApp(App).use(VueKeycloak) 4 5// You can also pass in options. Check options reference below. 6createApp(App).use(VueKeycloak, options)
1import VueKeyCloak from '@dsb-norge/vue-keycloak-js' 2import { VueKeycloakInstance } from "@dsb-norge/vue-keycloak-js/dist/types"; 3 4createApp(App).use(VueKeycloak) 5 6// You can also pass in options. Check options reference below. 7createApp(App).use(VueKeycloak, options) 8 9// Allow usage of this.$keycloak in components 10declare module '@vue/runtime-core' { 11 interface ComponentCustomProperties { 12 $keycloak: VueKeycloakInstance 13 } 14}
The plugin adds a $keycloak
property to the global Vue instance.
This shadows most of the keycloak instance's properties and functions.
All other variables & functions can be found on $keycloak.keycloak
attribute
Internally for Vue 2:
Internally for Vue 3
This object is reactive and will update with new tokens and other information
These properties/functions are exposed:
{
ready: Boolean, // Flag indicating whether Keycloak has initialised and is ready
authenticated: Boolean,
userName: String, // Username from Keycloak. Collected from tokenParsed['preferred_username']
fullName: String, // Full name from Keycloak. Collected from tokenParsed['name']
login: Function, // [Keycloak] login function
loginFn: Function, // Alias for login
logoutFn: Function, // Keycloak logout function
createLoginUrl: Function, // Keycloak createLoginUrl function
createLogoutUrl: Function, // Keycloak createLogoutUrl function
createRegisterUrl: Function, // Keycloak createRegisterUrl function
register: Function, // Keycloak register function
accountManagement: Function, // Keycloak accountManagement function
createAccountUrl: Function, // Keycloak createAccountUrl function
loadUserProfile: Function, // Keycloak loadUserProfile function
subject: String, // The user id
idToken: String, // The base64 encoded ID token.
idTokenParsed: Object, // The parsed id token as a JavaScript object.
realmAccess: Object, // The realm roles associated with the token.
resourceAccess: Object, // The resource roles associated with the token.
refreshToken: String, // The base64 encoded refresh token that can be used to retrieve a new token.
refreshTokenParsed: Object, // The parsed refresh token as a JavaScript object.
timeSkew: Number, // The estimated time difference between the browser time and the Keycloak server in seconds. This value is just an estimation, but is accurate enough when determining if a token is expired or not.
responseMode: String, // Response mode passed in init (default value is fragment).
responseType: String, // Response type sent to Keycloak with login requests. This is determined based on the flow value used during initialization, but can be overridden by setting this value.
hasRealmRole: Function, // Keycloak hasRealmRole function
hasResourceRole: Function, // Keycloak hasResourceRole function
token: String, // The base64 encoded token that can be sent in the Authorization header in requests to services
tokenParsed: String // The parsed token as a JavaScript object
keycloak: Object // The original keycloak instance 'as is' from keycloak-js adapter
}
Options
You can pass in an object as options to the plugin. The following keys are valid options. See below for description.
Key | Type | Default |
---|---|---|
config | String | Object | window.__BASEURL__ + '/config' |
init | Object | {onLoad: 'login-required'} |
logout | Object | |
onReady | Function(keycloak) | |
onInitError | Function(error, keycloakError) | |
onInitSuccess | Function(authenticated) | |
onAuthLogout | Function(keycloak) | |
onAuthRefreshError | Function(keycloak) | |
onAuthRefreshSuccess | Function(keycloak) |
config
The config object, either returned from an endpoint (string) or set directly (object), must be compatible with the Keycloak JS adapter constructor arguments.
See description below.
String
If this option is a string, the plugin will treat it as an URL and make an HTTP GET request to it.
If not present, the plugin will look for a global variable window.__BASEURL__
and prepend it to '/config'
and use
this a default place to make a GET request.
If no window.__BASEURL__
exists, /config
is used.
The return value from the request is used as constructor parameters for the Keycloak adapter. As such, it should be an object with valid keys/values.
See Keycloak's Javascript adapter reference
E.g.
{
realm: String,
url: String,
clientId: String
}
These values will be used as constructor parameters to the official Keycloak adapter.
Object
If this option is an object, it will be passed on as constructor parameters for the Keycloak adapter. No HTTP GET request is done in this case.
init
This option is the parameter object for the Keycloak.init
method.
logout
This option is the parameter object for the Keycloak.logout
method.
onReady
This option is a callback function that is executed once Keycloak has initialised and is ready. You can be sure
that the Vue instance has a property called $keycloak
in this function. See above for possible values.
The callback function has one parameter, which is the keycloak object returned from the Keycloak adapter on instantiation.
One use case for this callback could be to instantiate and mount the Vue application. Then we are sure that the Keycloak
authentication and the $keycloak
property are properly finished and hydrated with data:
1Vue.use(VueKeyCloak, { 2 onReady: (keycloak) => { 3 console.log(`I wonder what Keycloak returns: ${keycloak}`) 4 /* eslint-disable no-new */ 5 new Vue({ 6 el: '#app', 7 router, 8 template: '<App/>', 9 render: h => h(App) 10 }) 11 } 12})
In conjunction with the above, you might find it useful to intercept e.g. axios and set the token on each request:
1function tokenInterceptor () { 2 axios.interceptors.request.use(config => { 3 if (Vue.prototype.$keycloak.authenticated) { 4 config.headers.Authorization = `Bearer ${Vue.prototype.$keycloak.token}` 5 } 6 return config 7 }, error => { 8 return Promise.reject(error) 9 }) 10} 11 12Vue.use(VueKeyCloak, { 13 onReady: (keycloak) => { 14 tokenInterceptor() 15 /* eslint-disable no-new */ 16 new Vue({ 17 el: '#app', 18 router, 19 template: '<App/>', 20 render: h => h(App) 21 }) 22 } 23})
onInitError
This option is a callback function that is executed if Keycloak initialisation has failed.
The callback function has one parameter, which is the error object returned by Keycloak. Note that this may be undefined even though an error has occurred, as Keycloak does not return an error object in every error case.
onInitSuccess
This option is a callback function that is executed if Keycloak initialisation has succeeded.
The callback function has one parameter, which is the authenticated
value returned by Keycloak's init
method.
onAuthLogout
This option is a callback function that is executed if the user is logged out (will only be called if the session status iframe is enabled, or in Cordova mode).
The callback function has one parameter, which is the keycloak object returned from the Keycloak adapter on instantiation.
onAuthRefreshError
This option is a callback function that is executed if there was an error while trying to refresh the token.
The callback function has one parameter, which is the keycloak object returned from the Keycloak adapter on instantiation.
onAuthRefreshSuccess
This option is a callback function that is executed when the token is refreshed.
The callback function has one parameter, which is the keycloak object returned from the Keycloak adapter on instantiation.
Examples
Supply a configuration object for the Keycloak constructor
1Vue.use(VueKeyCloak, { 2 config: { 3 realm: 'MyRealm', 4 url: 'https://my.keycloak.server/auth', 5 clientId: 'MyClientId' 6 }, 7 onReady: kc => { 8 new Vue({ 9 render: h => h(App) 10 }).$mount('#app') 11 } 12})
Supply init option (disable monitoring login state)
1Vue.use(VueKeyCloak, { 2 init: { 3 onLoad: 'login-required', 4 checkLoginIframe: false 5 }, 6 onReady: kc => { 7 new Vue({ 8 render: h => h(App) 9 }).$mount('#app') 10 } 11})
Supply init option (use check-sso
)
Remember; login-required
is the default value for the onLoad property
in the init object. So without passing an init
object as argument, the default is
{ init: 'login-required' }
To avoid waiting for configuration endpoint before loading vue app:
1Vue.use(VueKeyCloak, { 2 init: { 3 onLoad: 'check-sso' 4 } 5}) 6 7new Vue({ 8 render: h => h(App) 9}).$mount('#app')
Wait until keycloak adapter is ready before loading vue app:
1Vue.use(VueKeyCloak, { 2 init: { 3 onLoad: 'check-sso' 4 }, 5 onReady: kc => { 6 new Vue({ 7 render: h => h(App) 8 }).$mount('#app') 9 } 10})
Specify a redirectUri
1Vue.use(VueKeyCloak, { 2 logout: { 3 redirectUri: 'https://mydomain.lives.here.com' 4 }, 5 onReady: kc => { 6 new Vue({ 7 render: h => h(App) 8 }).$mount('#app') 9 } 10})
Example applications
View a complete example app, with router guards:
Simple 'in component' secret displaying reactiveness:
Typescript example with vue 3
How to release
1$ git checkout main 2$ npm version [major | minor | patch ] 3$ git push 4$ git push --tags
Go to GitHub and create a new release based on the latest tag. GitHub Actions will then build and publish the release to npmjs.com.
Frequently asked questions
We try to answer the most frequently asked questions here.
Localhost !== 127.0.0.1
Note that if you run keycloak on your own machine it is important to be consistent with the settings for its address. Cookies created from 127.0.0.1 will not be sent to "localhost" and vice versa.
No vulnerabilities found.
Reason
no dangerous workflow patterns detected
Reason
no binaries found in the repo
Reason
license file detected
Details
- Info: project has a license file: LICENSE.md:0
- Info: FSF or OSI recognized license: ISC License: LICENSE.md:0
Reason
10 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 8
Reason
dependency not pinned by hash detected -- score normalized to 4
Details
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/action.yml:15: update your workflow using https://app.stepsecurity.io/secureworkflow/dsb-norge/vue-keycloak-js/action.yml/main?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/action.yml:18: update your workflow using https://app.stepsecurity.io/secureworkflow/dsb-norge/vue-keycloak-js/action.yml/main?enable=pin
- Warn: third-party GitHubAction not pinned by hash: .github/workflows/action.yml:26: update your workflow using https://app.stepsecurity.io/secureworkflow/dsb-norge/vue-keycloak-js/action.yml/main?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/publish.yml:16: update your workflow using https://app.stepsecurity.io/secureworkflow/dsb-norge/vue-keycloak-js/publish.yml/main?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/publish.yml:19: update your workflow using https://app.stepsecurity.io/secureworkflow/dsb-norge/vue-keycloak-js/publish.yml/main?enable=pin
- Warn: npmCommand not pinned by hash: .github/workflows/publish.yml:26
- Info: 0 out of 4 GitHub-owned GitHubAction dependencies pinned
- Info: 0 out of 1 third-party GitHubAction dependencies pinned
- Info: 2 out of 3 npmCommand dependencies pinned
Reason
Found 2/8 approved changesets -- score normalized to 2
Reason
detected GitHub workflow tokens with excessive permissions
Details
- Info: jobLevel 'contents' permission set to 'read': .github/workflows/publish.yml:12
- Warn: no topLevel permission defined: .github/workflows/action.yml:1
- Warn: no topLevel permission defined: .github/workflows/publish.yml:1
- Info: no jobLevel write permissions found
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Reason
branch protection not enabled on development/release branches
Details
- Warn: branch protection not enabled for branch 'main'
Reason
security policy file not detected
Details
- Warn: no security policy file detected
- Warn: no security file to analyze
- Warn: no security file to analyze
- Warn: no security file to analyze
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
- Warn: 0 commits out of 25 are checked with a SAST tool
Reason
34 existing vulnerabilities detected
Details
- Warn: Project is vulnerable to: GHSA-67hx-6x53-jw92
- Warn: Project is vulnerable to: GHSA-qwcr-r2fm-qrc7
- Warn: Project is vulnerable to: GHSA-grv7-fg5c-xmjg
- Warn: Project is vulnerable to: GHSA-pxg6-pf52-xh8x
- Warn: Project is vulnerable to: GHSA-3xgq-45jj-v275
- Warn: Project is vulnerable to: GHSA-rv95-896h-c2vc
- Warn: Project is vulnerable to: GHSA-qw6h-vgh9-j6wx
- Warn: Project is vulnerable to: GHSA-jchw-25xp-jwwc
- Warn: Project is vulnerable to: GHSA-cxjh-pqwp-8mfp
- Warn: Project is vulnerable to: GHSA-c7qv-q95q-8v27
- Warn: Project is vulnerable to: GHSA-952p-6rrq-rcjv
- Warn: Project is vulnerable to: GHSA-mwcw-c2x4-8c55
- Warn: Project is vulnerable to: GHSA-9wv6-86v2-598j
- Warn: Project is vulnerable to: GHSA-rhx6-c78j-4q9w
- Warn: Project is vulnerable to: GHSA-7fh5-64p2-3v2j
- Warn: Project is vulnerable to: GHSA-c2qf-rxjj-qqgw
- Warn: Project is vulnerable to: GHSA-m6fv-jmcg-4jfg
- Warn: Project is vulnerable to: GHSA-cm22-4g7w-348p
- Warn: Project is vulnerable to: GHSA-5j4c-8p2g-v4jx
- Warn: Project is vulnerable to: GHSA-g3ch-rx76-35fx
- Warn: Project is vulnerable to: GHSA-hc6q-2mpp-qw7j
- Warn: Project is vulnerable to: GHSA-4vvj-4cpr-p986 / GHSA-64vr-g452-qvp3
- Warn: Project is vulnerable to: GHSA-wr3j-pwj9-hqq6
- Warn: Project is vulnerable to: GHSA-3h5v-q93c-6h6q
- Warn: Project is vulnerable to: GHSA-x9w5-v3q2-3rhw
- Warn: Project is vulnerable to: GHSA-434g-2637-qmqr
- Warn: Project is vulnerable to: GHSA-49q7-c7j4-3p7m
- Warn: Project is vulnerable to: GHSA-977x-g7h5-7qgw
- Warn: Project is vulnerable to: GHSA-f7q4-pwc6-w24p
- Warn: Project is vulnerable to: GHSA-fc9h-whq2-v747
- Warn: Project is vulnerable to: GHSA-gcx4-mw62-g8wm
- Warn: Project is vulnerable to: GHSA-c24v-8rfc-w8vw
- Warn: Project is vulnerable to: GHSA-8jhw-289h-jh2g
- Warn: Project is vulnerable to: GHSA-9cwx-2883-4wfx
Score
3.7
/10
Last Scanned on 2024-12-23
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More