Gathering detailed insights and metrics for @dsb-norge/vue-keycloak-js
Gathering detailed insights and metrics for @dsb-norge/vue-keycloak-js
Gathering detailed insights and metrics for @dsb-norge/vue-keycloak-js
Gathering detailed insights and metrics for @dsb-norge/vue-keycloak-js
npm install @dsb-norge/vue-keycloak-js
Typescript
Module System
Node Version
NPM Version
65.6
Supply Chain
99.1
Quality
81.8
Maintenance
100
Vulnerability
100
License
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
TypeScript (86.3%)
JavaScript (13.7%)
Total Downloads
983,388
Last Day
204
Last Week
5,143
Last Month
23,607
Last Year
293,721
273 Stars
191 Commits
99 Forks
13 Watching
1 Branches
16 Contributors
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
Cumulative downloads
Total Downloads
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
1
1
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
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' })
npm install @dsb-norge/vue-keycloak-js --save
yarn add @dsb-norge/vue-keycloak-js
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)
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
}
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) |
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.
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.
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.
This option is the parameter object for the Keycloak.init
method.
This option is the parameter object for the Keycloak.logout
method.
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})
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.
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.
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.
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.
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.
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})
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})
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' }
1Vue.use(VueKeyCloak, { 2 init: { 3 onLoad: 'check-sso' 4 } 5}) 6 7new Vue({ 8 render: h => h(App) 9}).$mount('#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})
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})
View a complete example app, with router guards:
Simple 'in component' secret displaying reactiveness:
Typescript example with vue 3
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.
We try to answer the most frequently asked questions here.
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
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
Reason
Found 2/8 approved changesets -- score normalized to 2
Reason
detected GitHub workflow tokens with excessive permissions
Details
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
security policy file not detected
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Reason
34 existing vulnerabilities detected
Details
Score
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