├── .prettierignore ├── src ├── const.ts ├── const.test.ts ├── index.ts ├── config.ts ├── utils.ts ├── state.test.ts ├── composable.ts ├── state.ts ├── utils.test.ts ├── plugin.ts ├── composable.test.ts ├── keycloak.ts ├── plugin.test.ts └── keycloak.test.ts ├── jest.config.js ├── .editorconfig ├── .prettierrc ├── rollup.config.js ├── .releaserc ├── .github └── workflows │ ├── continous.yml │ └── release.yml ├── .eslintrc.js ├── tsconfig.json ├── CHANGELOG.md ├── .gitignore ├── package.json ├── README.md └── LICENSE /.prettierignore: -------------------------------------------------------------------------------- 1 | node_modules/ 2 | dist/ 3 | -------------------------------------------------------------------------------- /src/const.ts: -------------------------------------------------------------------------------- 1 | import Keycloak from 'keycloak-js' 2 | 3 | export const defaultInitConfig: Keycloak.KeycloakInitOptions = { 4 | flow: 'standard', 5 | checkLoginIframe: false, 6 | onLoad: 'login-required', 7 | } 8 | -------------------------------------------------------------------------------- /jest.config.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | roots: ['/src'], 3 | testMatch: ['**/__tests__/**/*.+(ts|tsx|js)', '**/?(*.)+(spec|test).+(ts|tsx|js)'], 4 | transform: { 5 | '^.+\\.(ts|tsx)$': 'ts-jest', 6 | }, 7 | } 8 | -------------------------------------------------------------------------------- /.editorconfig: -------------------------------------------------------------------------------- 1 | # http://editorconfig.org 2 | 3 | root = true 4 | 5 | [*] 6 | charset = utf-8 7 | indent_style = space 8 | indent_size = 2 9 | end_of_line = lf 10 | insert_final_newline = true 11 | trim_trailing_whitespace = true 12 | 13 | [*.md] 14 | insert_final_newline = false 15 | trim_trailing_whitespace = false -------------------------------------------------------------------------------- /.prettierrc: -------------------------------------------------------------------------------- 1 | { 2 | "singleQuote": true, 3 | "semi": false, 4 | "jsxBracketSameLine": true, 5 | "trailingComma": "all", 6 | "arrowParens": "avoid", 7 | "bracketSpacing": true, 8 | "jsxSingleQuote": false, 9 | "quoteProps": "consistent", 10 | "printWidth": 120, 11 | "tabWidth": 2, 12 | "useTabs": false 13 | } 14 | -------------------------------------------------------------------------------- /src/const.test.ts: -------------------------------------------------------------------------------- 1 | import { defaultInitConfig } from './const' 2 | 3 | describe('defaultInitConfig', () => { 4 | test('should habe the standard baloise config', () => { 5 | expect(defaultInitConfig.flow).toBe('standard') 6 | expect(defaultInitConfig.checkLoginIframe).toBe(false) 7 | expect(defaultInitConfig.onLoad).toBe('login-required') 8 | }) 9 | }) 10 | -------------------------------------------------------------------------------- /rollup.config.js: -------------------------------------------------------------------------------- 1 | export default { 2 | input: 'dist-transpiled/index.js', 3 | output: [ 4 | { 5 | dir: 'dist/', 6 | entryFileNames: '[name].esm.js', 7 | chunkFileNames: '[name]-[hash].esm.js', 8 | format: 'es', 9 | sourcemap: true, 10 | }, 11 | { 12 | dir: 'dist/', 13 | format: 'commonjs', 14 | preferConst: true, 15 | sourcemap: true, 16 | }, 17 | ], 18 | external: ['keycloak-js', 'jwt-decode', 'vue'], 19 | } 20 | -------------------------------------------------------------------------------- /src/index.ts: -------------------------------------------------------------------------------- 1 | /* ============ 2 | * Keycloak 3 | * ============ 4 | * 5 | * Keycloak comes with a client-side JavaScript library that can be used to secure HTML5/JavaScript applications. 6 | * The JavaScript adapter has built-in support for Cordova applications. 7 | * 8 | * https://www.keycloak.org/docs/latest/securing_apps/#_javascript_adapter 9 | */ 10 | 11 | export { getToken, getKeycloak, isTokenReady } from './keycloak' 12 | export * from './composable' 13 | export * from './plugin' 14 | -------------------------------------------------------------------------------- /.releaserc: -------------------------------------------------------------------------------- 1 | { 2 | "plugins": [ 3 | "@semantic-release/commit-analyzer", 4 | "@semantic-release/release-notes-generator", 5 | ["@semantic-release/npm", { "npmPublish": true }], 6 | "@semantic-release/changelog", 7 | [ 8 | "@semantic-release/git", 9 | { 10 | "assets": ["package.json", "package-lock.json", "CHANGELOG.md"], 11 | "message": "chore(release): ${nextRelease.version} [skip ci]\n\n${nextRelease.notes}" 12 | } 13 | ], 14 | "@semantic-release/github" 15 | ] 16 | } 17 | -------------------------------------------------------------------------------- /.github/workflows/continous.yml: -------------------------------------------------------------------------------- 1 | name: Continous 2 | 3 | on: [push] 4 | 5 | jobs: 6 | BuildAndTest: 7 | runs-on: ubuntu-latest 8 | steps: 9 | - name: Checkout Repository 10 | uses: actions/checkout@v1 11 | 12 | - name: Clean install dependencies 13 | run: npm ci 14 | 15 | - name: Format 16 | run: npm run prettier:check 17 | 18 | - name: Lint 19 | run: npm run lint 20 | 21 | - name: Build 22 | run: npm run build 23 | 24 | - name: Test 25 | run: npm run test 26 | -------------------------------------------------------------------------------- /src/config.ts: -------------------------------------------------------------------------------- 1 | export function loadJsonConfig(url: string): Promise { 2 | return new Promise((resolve, reject) => { 3 | const xhttp = new XMLHttpRequest() 4 | xhttp.overrideMimeType('application/json') 5 | xhttp.onreadystatechange = function () { 6 | if (this.readyState == 4 && this.status == 200) { 7 | const jsonResponse = this.responseText 8 | const response = JSON.parse(jsonResponse) 9 | resolve(response) 10 | } else { 11 | reject('Could not load ' + url + ' file') 12 | } 13 | } 14 | xhttp.open('GET', url, true) 15 | xhttp.send() 16 | }) 17 | } 18 | -------------------------------------------------------------------------------- /.eslintrc.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | root: true, 3 | env: { 4 | node: true, 5 | }, 6 | plugins: ['vue'], 7 | extends: [ 8 | 'plugin:vue/vue3-essential', 9 | 'eslint:recommended', 10 | '@vue/typescript/recommended', 11 | '@vue/prettier', 12 | '@vue/prettier/@typescript-eslint', 13 | ], 14 | parserOptions: { 15 | ecmaVersion: 2020, 16 | }, 17 | overrides: [ 18 | { 19 | files: ['**/src/**/*.test.{j,t}s?(x)'], 20 | env: { 21 | jest: true, 22 | }, 23 | rules: { 24 | '@typescript-eslint/no-explicit-any': 'off', 25 | '@typescript-eslint/no-unused-vars': 'off', 26 | }, 27 | }, 28 | ], 29 | } 30 | -------------------------------------------------------------------------------- /tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "allowUnreachableCode": false, 4 | "allowSyntheticDefaultImports": true, 5 | "declaration": true, 6 | "emitDecoratorMetadata": true, 7 | "experimentalDecorators": true, 8 | "esModuleInterop": true, 9 | "lib": ["dom", "es2020"], 10 | "module": "es2015", 11 | "target": "es2017", 12 | "moduleResolution": "node", 13 | "noImplicitAny": true, 14 | "noImplicitReturns": true, 15 | "noUnusedLocals": true, 16 | "noUnusedParameters": true, 17 | "outDir": "dist-transpiled", 18 | "declarationDir": "dist/types", 19 | "removeComments": false, 20 | "sourceMap": true, 21 | "jsx": "react", 22 | "types": ["@types/jest"] 23 | }, 24 | "include": ["src/**/*.ts", "src/**/*.tsx"], 25 | "exclude": ["node_modules", "src/**/*.test.ts", "src/**/*.spec.ts"], 26 | "compileOnSave": false, 27 | "buildOnSave": false 28 | } 29 | -------------------------------------------------------------------------------- /src/utils.ts: -------------------------------------------------------------------------------- 1 | // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types, @typescript-eslint/no-explicit-any 2 | export function isPromise(promise: any): boolean { 3 | return !isNil(promise) && typeof promise.then === 'function' 4 | } 5 | 6 | // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types, @typescript-eslint/no-explicit-any 7 | export function isFunction(fun: any): boolean { 8 | return !isNil(fun) && typeof fun === 'function' 9 | } 10 | 11 | // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types, @typescript-eslint/no-explicit-any 12 | export function isString(text: any): boolean { 13 | return !isNil(text) && (typeof text === 'string' || text instanceof String) 14 | } 15 | 16 | // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types, @typescript-eslint/no-explicit-any 17 | export function isNil(value: any): boolean { 18 | return value === undefined || value === null 19 | } 20 | -------------------------------------------------------------------------------- /src/state.test.ts: -------------------------------------------------------------------------------- 1 | import { state, setToken } from './state' 2 | 3 | describe('state', () => { 4 | const token = 5 | 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyLCJwcmVmZXJyZWRfdXNlcm5hbWUiOiJteS1uYW1lIiwicmVhbG1fYWNjZXNzIjp7InJvbGVzIjpbIm15LXJvbGUiXX0sInJlc291cmNlX2FjY2VzcyI6eyJteS1hcHAiOnsicm9sZXMiOlsibXktcm9sZSJdfX19.oAnF7H8DndIWOb2KeHntbzwf6h7VjZlxt5AR2KPZTBU' 6 | 7 | test('should have the correct inital values', () => { 8 | expect(state.isAuthenticated).toBe(false) 9 | expect(state.hasFailed).toBe(false) 10 | expect(state.isPending).toBe(false) 11 | expect(state.token).toBe('') 12 | expect(state.username).toBe('') 13 | expect(state.roles).toStrictEqual([]) 14 | expect(state.resourceRoles).toStrictEqual({}); 15 | }) 16 | 17 | test('should update the state', () => { 18 | setToken(token) 19 | 20 | expect(state.token).toBe(token) 21 | expect(state.username).toBe('my-name') 22 | expect(state.roles).toStrictEqual(['my-role']) 23 | expect(state.resourceRoles).toStrictEqual({ 'my-app': ['my-role'] }) 24 | }) 25 | }) 26 | -------------------------------------------------------------------------------- /src/composable.ts: -------------------------------------------------------------------------------- 1 | import { KeycloakInstance } from 'keycloak-js' 2 | import { toRefs, Ref } from 'vue' 3 | import { getKeycloak } from './keycloak' 4 | import { KeycloakState, state } from './state' 5 | import { isNil } from './utils' 6 | 7 | export interface KeycloakComposable { 8 | isAuthenticated: Ref 9 | hasFailed: Ref 10 | isPending: Ref 11 | token: Ref 12 | username: Ref 13 | roles: Ref 14 | resourceRoles: Ref> 15 | keycloak: KeycloakInstance 16 | hasRoles: (roles: string[]) => boolean 17 | hasResourceRoles: (roles: string[], resource: string) => boolean 18 | } 19 | 20 | export const useKeycloak = (): KeycloakComposable => { 21 | return { 22 | ...toRefs(state), 23 | keycloak: getKeycloak(), 24 | hasRoles: (roles: string[]) => 25 | !isNil(roles) && state.isAuthenticated && roles.every(role => state.roles.includes(role)), 26 | hasResourceRoles: (roles: string[], resource: string) => 27 | !isNil(roles) && 28 | !isNil(resource) && 29 | state.isAuthenticated && 30 | roles.every(role => state.resourceRoles[resource].includes(role)), 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # [1.4.0](https://github.com/baloise/vue-keycloak/compare/v1.3.0...v1.4.0) (2021-08-02) 2 | 3 | 4 | ### Features 5 | 6 | * add support for resource roles ([7908cf0](https://github.com/baloise/vue-keycloak/commit/7908cf0629ba4998b1ea49253ff3f309a6a2bbed)) 7 | 8 | # [1.3.0](https://github.com/baloise/vue-keycloak/compare/v1.2.0...v1.3.0) (2021-04-07) 9 | 10 | 11 | ### Features 12 | 13 | * add load json config and add keycloak instance to the composable ([92b4d2b](https://github.com/baloise/vue-keycloak/commit/92b4d2b729ad8652d1fdb5a513d22188c68538d7)) 14 | 15 | # [1.2.0](https://github.com/baloise/vue-keycloak/compare/v1.1.0...v1.2.0) (2021-04-06) 16 | 17 | 18 | ### Features 19 | 20 | * add decodedToken ([f778338](https://github.com/baloise/vue-keycloak/commit/f778338b5853d695bab7dc5a1a411b54f9d07e34)) 21 | 22 | # [1.1.0](https://github.com/baloise/vue-keycloak/compare/v1.0.0...v1.1.0) (2021-04-04) 23 | 24 | 25 | ### Features 26 | 27 | * add string config ([17beca6](https://github.com/baloise/vue-keycloak/commit/17beca6daee78b098e665d422d84518120421baa)) 28 | 29 | # 1.0.0 (2021-04-04) 30 | 31 | 32 | ### Features 33 | 34 | * add utils and tests ([0a7c90b](https://github.com/baloise/vue-keycloak/commit/0a7c90b04d446b117f2de7b16c35c117484411aa)) 35 | -------------------------------------------------------------------------------- /src/state.ts: -------------------------------------------------------------------------------- 1 | import { reactive } from 'vue' 2 | import jwtDecode from 'jwt-decode' 3 | 4 | export interface KeycloakState { 5 | isAuthenticated: boolean 6 | hasFailed: boolean 7 | isPending: boolean 8 | token: string 9 | decodedToken: T 10 | username: string 11 | roles: string[] 12 | resourceRoles: Record 13 | } 14 | 15 | export const state = reactive({ 16 | isAuthenticated: false, 17 | hasFailed: false, 18 | isPending: false, 19 | token: '', 20 | decodedToken: {}, 21 | username: '', 22 | roles: [] as string[], 23 | resourceRoles: {}, 24 | }) 25 | 26 | interface TokenContent { 27 | preferred_username: string 28 | realm_access: { 29 | roles: string[] 30 | } 31 | resource_access: Record 32 | } 33 | 34 | export const setToken = (token: string): void => { 35 | state.token = token 36 | const content = jwtDecode(state.token) 37 | state.decodedToken = content 38 | state.roles = content.realm_access.roles 39 | state.username = content.preferred_username 40 | state.resourceRoles = content.resource_access ? Object.fromEntries( 41 | Object.entries(content.resource_access).map(([key, value]) => [key, value.roles]), 42 | ) : {}; 43 | } 44 | 45 | export const hasFailed = (value: boolean): void => { 46 | state.hasFailed = value 47 | } 48 | 49 | export const isPending = (value: boolean): void => { 50 | state.isPending = value 51 | } 52 | 53 | export const isAuthenticated = (value: boolean): void => { 54 | state.isAuthenticated = value 55 | } 56 | -------------------------------------------------------------------------------- /.github/workflows/release.yml: -------------------------------------------------------------------------------- 1 | name: Release 2 | 3 | on: 4 | push: 5 | branches: 6 | - master 7 | 8 | jobs: 9 | Publish: 10 | runs-on: ubuntu-latest 11 | steps: 12 | - name: Checkout Repository 13 | uses: actions/checkout@v2 14 | with: 15 | fetch-depth: '0' 16 | 17 | - name: Git Identity 18 | run: | 19 | git config --global user.name 'baopso' 20 | git config --global user.email 'Group.CH_Open-Source@baloise.ch' 21 | git remote set-url origin https://$GITHUB_ACTOR:$GITHUB_TOKEN@github.com/$GITHUB_REPOSITORY 22 | env: 23 | GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} 24 | 25 | # - name: Fetch all Tags and Pull 26 | # run: | 27 | # git fetch --depth=1 origin +refs/tags/*:refs/tags/* 28 | # git pull 29 | 30 | - name: Authenticate with Registry 31 | run: | 32 | echo "registry=http://registry.npmjs.org/" > .npmrc 33 | echo //registry.npmjs.org/:_authToken=$NPM_PUBLISH_TOKEN >> .npmrc 34 | env: 35 | NPM_TOKEN: ${{ secrets.NPM_PUBLISH_TOKEN }} 36 | 37 | - name: Clean install dependencies 38 | run: npm ci 39 | 40 | - name: Build 41 | run: npm run build 42 | 43 | # - uses: EndBug/add-and-commit@v5 44 | # with: 45 | # message: 'chore(): update build artifacts' 46 | # env: 47 | # GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} 48 | 49 | - name: Publish to NPM 50 | run: 'npm run release' 51 | env: 52 | NPM_TOKEN: ${{ secrets.NPM_PUBLISH_TOKEN }} 53 | GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} 54 | GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} 55 | -------------------------------------------------------------------------------- /src/utils.test.ts: -------------------------------------------------------------------------------- 1 | import { isPromise, isFunction, isNil, isString } from './utils' 2 | 3 | describe('util', () => { 4 | const obj = {} 5 | const fun = (): void => undefined 6 | const prom = new Promise(() => undefined) 7 | 8 | describe('isPromise', () => { 9 | test('should return true if it is a valid promise', () => { 10 | expect(isPromise(prom)).toBe(true) 11 | expect(isPromise(fun)).toBe(false) 12 | expect(isPromise(obj)).toBe(false) 13 | expect(isPromise(undefined)).toBe(false) 14 | expect(isPromise(null)).toBe(false) 15 | }) 16 | }) 17 | 18 | describe('isFunction', () => { 19 | test('should return true if it is a valid function', () => { 20 | expect(isFunction(fun)).toBe(true) 21 | expect(isFunction(prom)).toBe(false) 22 | expect(isFunction(obj)).toBe(false) 23 | expect(isFunction(undefined)).toBe(false) 24 | expect(isFunction(null)).toBe(false) 25 | }) 26 | }) 27 | 28 | describe('isString', () => { 29 | test('should return true if it is a valid string', () => { 30 | expect(isString('')).toBe(true) 31 | expect(isString('adsf')).toBe(true) 32 | expect(isString(undefined)).toBe(false) 33 | expect(isString(null)).toBe(false) 34 | expect(isString(fun)).toBe(false) 35 | expect(isString(prom)).toBe(false) 36 | expect(isString(obj)).toBe(false) 37 | }) 38 | }) 39 | 40 | describe('isNil', () => { 41 | test('should return true if it is null or undefined', () => { 42 | expect(isNil(undefined)).toBe(true) 43 | expect(isNil(null)).toBe(true) 44 | expect(isNil(fun)).toBe(false) 45 | expect(isNil(prom)).toBe(false) 46 | expect(isNil(obj)).toBe(false) 47 | }) 48 | }) 49 | }) 50 | -------------------------------------------------------------------------------- /src/plugin.ts: -------------------------------------------------------------------------------- 1 | import { Plugin } from 'vue' 2 | import Keycloak from 'keycloak-js' 3 | import { defaultInitConfig } from './const' 4 | import { createKeycloak, initKeycloak } from './keycloak' 5 | import { isPromise, isFunction, isNil, isString } from './utils' 6 | import { loadJsonConfig } from './config' 7 | 8 | interface KeycloakPluginConfig { 9 | config: Keycloak.KeycloakConfig 10 | initOptions?: Keycloak.KeycloakInitOptions 11 | } 12 | 13 | type KeycloakConfigFactory = () => KeycloakPluginConfig 14 | type KeycloakConfigAsyncFactory = () => Promise 15 | 16 | type VueKeycloakPluginConfig = string | KeycloakPluginConfig | KeycloakConfigFactory | KeycloakConfigAsyncFactory 17 | 18 | export const vueKeycloak: Plugin = { 19 | async install(app, options: VueKeycloakPluginConfig) { 20 | if (isNil(options)) { 21 | throw new Error('The Keycloak.KeycloakConfig are requried') 22 | } 23 | 24 | let keycloakPluginConfig: KeycloakPluginConfig 25 | if (isString(options)) { 26 | keycloakPluginConfig = await loadJsonConfig(options as string) 27 | } else if (isPromise(options) || isFunction(options)) { 28 | keycloakPluginConfig = await (options as KeycloakConfigAsyncFactory)() 29 | } else { 30 | keycloakPluginConfig = options as KeycloakPluginConfig 31 | } 32 | 33 | const keycloakConfig = keycloakPluginConfig.config 34 | const keycloakInitOptions: Keycloak.KeycloakInitOptions = !isNil(keycloakPluginConfig.initOptions) 35 | ? { ...defaultInitConfig, ...keycloakPluginConfig.initOptions } 36 | : defaultInitConfig 37 | 38 | const _keycloak = createKeycloak(keycloakConfig) 39 | app.config.globalProperties.$keycloak = _keycloak 40 | 41 | await initKeycloak(keycloakInitOptions) 42 | }, 43 | } 44 | -------------------------------------------------------------------------------- /src/composable.test.ts: -------------------------------------------------------------------------------- 1 | import { useKeycloak } from './composable' 2 | import { state } from './state' 3 | 4 | describe('useKeycloak', () => { 5 | describe('state', () => { 6 | test('should return the state values as refs', () => { 7 | const { isAuthenticated, hasFailed, isPending, token, username, roles } = useKeycloak() 8 | 9 | expect(isAuthenticated.value).toBe(false) 10 | expect(hasFailed.value).toBe(false) 11 | expect(isPending.value).toBe(false) 12 | expect(token.value).toBe('') 13 | expect(username.value).toBe('') 14 | expect(roles.value).toStrictEqual([]) 15 | }) 16 | }) 17 | describe('hasRoles', () => { 18 | test('should tell if the user has the role or not and is authenticated', () => { 19 | state.isAuthenticated = true 20 | state.roles = ['my-role', 'my-other-role'] 21 | const { hasRoles } = useKeycloak() 22 | 23 | expect(hasRoles(['my-role', 'my-other-role'])).toBe(true) 24 | expect(hasRoles(['my-role', 'not-my-role'])).toBe(false) 25 | expect(hasRoles(undefined)).toBe(false) 26 | expect(hasRoles(null)).toBe(false) 27 | }) 28 | }) 29 | describe('hasResourceRoles', () => { 30 | test('should tell if the user has the role in a resource or not and is authenticated', () => { 31 | state.isAuthenticated = true 32 | state.resourceRoles = { myApp: ['my-role', 'my-other-role'] } 33 | const { hasResourceRoles } = useKeycloak() 34 | 35 | expect(hasResourceRoles(['my-role', 'my-other-role'], 'myApp')).toBe(true) 36 | expect(hasResourceRoles(['my-role', 'not-my-role'], 'myApp')).toBe(false) 37 | expect(hasResourceRoles(['my-role', 'my-other-role'], undefined)).toBe(false) 38 | expect(hasResourceRoles(['my-role', 'my-other-role'], null)).toBe(false) 39 | expect(hasResourceRoles(undefined, undefined)).toBe(false) 40 | expect(hasResourceRoles(undefined, 'myApp')).toBe(false) 41 | expect(hasResourceRoles(null, null)).toBe(false) 42 | expect(hasResourceRoles(null, 'myApp')).toBe(false) 43 | }) 44 | }) 45 | }) 46 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | npm-debug.log* 5 | yarn-debug.log* 6 | yarn-error.log* 7 | lerna-debug.log* 8 | 9 | # Diagnostic reports (https://nodejs.org/api/report.html) 10 | report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json 11 | 12 | # Runtime data 13 | pids 14 | *.pid 15 | *.seed 16 | *.pid.lock 17 | 18 | # Directory for instrumented libs generated by jscoverage/JSCover 19 | lib-cov 20 | 21 | # Coverage directory used by tools like istanbul 22 | coverage 23 | *.lcov 24 | 25 | # nyc test coverage 26 | .nyc_output 27 | 28 | # Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files) 29 | .grunt 30 | 31 | # Bower dependency directory (https://bower.io/) 32 | bower_components 33 | 34 | # node-waf configuration 35 | .lock-wscript 36 | 37 | # Compiled binary addons (https://nodejs.org/api/addons.html) 38 | build/Release 39 | 40 | # Dependency directories 41 | node_modules/ 42 | jspm_packages/ 43 | 44 | # TypeScript v1 declaration files 45 | typings/ 46 | 47 | # TypeScript cache 48 | *.tsbuildinfo 49 | 50 | # Optional npm cache directory 51 | .npm 52 | .npmrc 53 | 54 | # Optional eslint cache 55 | .eslintcache 56 | 57 | # Microbundle cache 58 | .rpt2_cache/ 59 | .rts2_cache_cjs/ 60 | .rts2_cache_es/ 61 | .rts2_cache_umd/ 62 | 63 | # Optional REPL history 64 | .node_repl_history 65 | 66 | # Output of 'npm pack' 67 | *.tgz 68 | 69 | # Yarn Integrity file 70 | .yarn-integrity 71 | 72 | # dotenv environment variables file 73 | .env 74 | .env.test 75 | 76 | # parcel-bundler cache (https://parceljs.org/) 77 | .cache 78 | 79 | # Next.js build output 80 | .next 81 | 82 | # Nuxt.js build / generate output 83 | .nuxt 84 | dist 85 | dist-transpiled 86 | 87 | 88 | # Gatsby files 89 | .cache/ 90 | # Comment in the public line in if your project uses Gatsby and *not* Next.js 91 | # https://nextjs.org/blog/next-9-1#public-directory-support 92 | # public 93 | 94 | # vuepress build output 95 | .vuepress/dist 96 | 97 | # Serverless directories 98 | .serverless/ 99 | 100 | # FuseBox cache 101 | .fusebox/ 102 | 103 | # DynamoDB Local files 104 | .dynamodb/ 105 | 106 | # TernJS port file 107 | .tern-port 108 | .idea 109 | /ui-library.iml 110 | /temp 111 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "@baloise/vue-keycloak", 3 | "version": "1.4.0", 4 | "description": "Keycloak plugin for Vue3 and Composition API", 5 | "author": { 6 | "name": "Gery Hirschfeld", 7 | "email": "gerhard.hirschfeld@baloise.ch", 8 | "url": "https://github.com/hirsch88" 9 | }, 10 | "homepage": "https://github.com/baloise/vue-keycloak", 11 | "repository": { 12 | "type": "git", 13 | "url": "https://github.com/baloise/vue-keycloak.git" 14 | }, 15 | "scripts": { 16 | "test": "jest", 17 | "test:watch": "jest --watchAll", 18 | "build": "npm run build:clean && npm run build:compile && npm run build:bundle", 19 | "build:clean": "rimraf dist && rimraf dist-transpiled", 20 | "build:compile": "tsc -p .", 21 | "build:bundle": "rollup --config rollup.config.js", 22 | "lint": "eslint src --ext .ts,vue", 23 | "format": "npm run prettier:write", 24 | "prettier:write": "prettier --write \"./src\"", 25 | "prettier:check": "prettier --check \"./src\"", 26 | "release": "semantic-release" 27 | }, 28 | "main": "./dist/index.js", 29 | "module": "./dist/index.esm.js", 30 | "types": "./dist/types/index.d.ts", 31 | "files": [ 32 | "dist/" 33 | ], 34 | "keywords": [ 35 | "vue", 36 | "keycloak", 37 | "composition-api" 38 | ], 39 | "license": "Apache-2.0", 40 | "peerDependencies": { 41 | "jwt-decode": "^3.1.2", 42 | "keycloak-js": "^12.0.4", 43 | "vue": "^3.0.8" 44 | }, 45 | "devDependencies": { 46 | "@semantic-release/changelog": "^5.0.1", 47 | "@semantic-release/git": "^9.0.0", 48 | "@semantic-release/github": "^7.2.0", 49 | "@semantic-release/npm": "^7.1.0", 50 | "@types/jest": "^26.0.22", 51 | "@typescript-eslint/eslint-plugin": "^4.19.0", 52 | "@typescript-eslint/parser": "^4.19.0", 53 | "@vue/eslint-config-prettier": "^6.0.0", 54 | "@vue/eslint-config-typescript": "^7.0.0", 55 | "eslint": "^7.23.0", 56 | "eslint-plugin-prettier": "^3.3.1", 57 | "eslint-plugin-vue": "^7.8.0", 58 | "jest": "^26.6.3", 59 | "jwt-decode": "^3.1.2", 60 | "keycloak-js": "^12.0.4", 61 | "prettier": "^2.2.1", 62 | "rimraf": "^3.0.2", 63 | "rollup": "^2.43.0", 64 | "semantic-release": "^17.4.2", 65 | "ts-jest": "^26.5.4", 66 | "typescript": "^4.2.3", 67 | "vue": "^3.0.8" 68 | }, 69 | "dependencies": {} 70 | } 71 | -------------------------------------------------------------------------------- /src/keycloak.ts: -------------------------------------------------------------------------------- 1 | import Keycloak from 'keycloak-js' 2 | import { hasFailed, isAuthenticated, isPending, setToken } from './state' 3 | import { isNil } from './utils' 4 | 5 | type KeycloakInstance = Keycloak.KeycloakInstance | undefined 6 | 7 | let $keycloak: KeycloakInstance = undefined 8 | 9 | export async function isTokenReady(): Promise { 10 | return new Promise(resolve => checkToken(resolve)) 11 | } 12 | 13 | const checkToken = (resolve: () => void) => { 14 | if (!isNil($keycloak) && !isNil($keycloak.token)) { 15 | resolve() 16 | } else { 17 | setTimeout(() => checkToken(resolve), 500) 18 | } 19 | } 20 | 21 | export function getKeycloak(): Keycloak.KeycloakInstance { 22 | return $keycloak as Keycloak.KeycloakInstance 23 | } 24 | 25 | export async function getToken(): Promise { 26 | return updateToken() 27 | } 28 | 29 | export async function isLoggedIn(): Promise { 30 | try { 31 | if (!$keycloak.authenticated) { 32 | return false 33 | } 34 | await this.updateToken() 35 | return true 36 | } catch (error) { 37 | return false 38 | } 39 | } 40 | 41 | export async function updateToken(): Promise { 42 | if (!$keycloak) { 43 | throw new Error('Keycloak is not initialized.') 44 | } 45 | 46 | try { 47 | await $keycloak.updateToken(10) 48 | setToken($keycloak.token as string) 49 | } catch (error) { 50 | hasFailed(true) 51 | throw new Error('Failed to refresh the token, or the session has expired') 52 | } 53 | return $keycloak.token 54 | } 55 | 56 | export function createKeycloak(config: Keycloak.KeycloakConfig | string): Keycloak.KeycloakInstance { 57 | $keycloak = Keycloak(config) 58 | return getKeycloak() 59 | } 60 | 61 | export async function initKeycloak(initConfig: Keycloak.KeycloakInitOptions): Promise { 62 | try { 63 | isPending(true) 64 | const _isAuthenticated = await $keycloak.init(initConfig) 65 | isAuthenticated(_isAuthenticated) 66 | if (!isNil($keycloak.token)) { 67 | setToken($keycloak.token as string) 68 | } 69 | 70 | $keycloak.onAuthRefreshSuccess = () => setToken($keycloak.token as string) 71 | $keycloak.onTokenExpired = () => updateToken() 72 | } catch (error) { 73 | hasFailed(true) 74 | isAuthenticated(false) 75 | throw new Error('Could not read access token') 76 | } finally { 77 | isPending(false) 78 | } 79 | } 80 | -------------------------------------------------------------------------------- /src/plugin.test.ts: -------------------------------------------------------------------------------- 1 | import { KeycloakConfig } from 'keycloak-js' 2 | import { vueKeycloak } from './plugin' 3 | import { createKeycloak, initKeycloak } from './keycloak' 4 | import { defaultInitConfig } from './const' 5 | 6 | jest.mock('./keycloak', () => { 7 | return { 8 | initKeycloak: jest.fn(), 9 | createKeycloak: jest.fn(), 10 | } 11 | }) 12 | 13 | describe('vueKeycloak', () => { 14 | let appMock: any 15 | const keycloakConfig: KeycloakConfig = { 16 | clientId: 'abc', 17 | realm: 'abc', 18 | url: 'abc', 19 | } 20 | 21 | beforeEach(() => { 22 | appMock = { 23 | config: { 24 | globalProperties: { 25 | $keycloak: undefined, 26 | }, 27 | }, 28 | } 29 | ;(createKeycloak as jest.Mock).mockClear() 30 | ;(initKeycloak as jest.Mock).mockClear() 31 | ;(createKeycloak as jest.Mock).mockImplementation(() => ({ isMyKeycloak: true })) 32 | ;(initKeycloak as jest.Mock).mockImplementation(() => undefined) 33 | }) 34 | 35 | test('should throw an error if config is nil', async () => { 36 | try { 37 | await vueKeycloak.install(appMock) 38 | } catch (error) { 39 | expect(error.message).toBe('The Keycloak.KeycloakConfig are requried') 40 | } 41 | }) 42 | 43 | test('should throw an error if client id is nil', async () => { 44 | try { 45 | await vueKeycloak.install(appMock, { config: {} }) 46 | } catch (error) { 47 | expect(error.message).toBe('Client ID is missing in Keycloak.KeycloakConfig') 48 | } 49 | }) 50 | 51 | test('should throw an error if realm is nil', async () => { 52 | try { 53 | await vueKeycloak.install(appMock, { config: { clientId: 'abc' } }) 54 | } catch (error) { 55 | expect(error.message).toBe('REALM is missing in Keycloak.KeycloakConfig') 56 | } 57 | }) 58 | 59 | test('should throw an error if url is nil', async () => { 60 | try { 61 | await vueKeycloak.install(appMock, { config: { clientId: 'abc', realm: 'abc' } }) 62 | } catch (error) { 63 | expect(error.message).toBe('URL is missing in Keycloak.KeycloakConfig') 64 | } 65 | }) 66 | 67 | test('should set globalProperties', async () => { 68 | await vueKeycloak.install(appMock, { config: keycloakConfig }) 69 | 70 | expect(appMock.config.globalProperties.$keycloak).toBeDefined() 71 | expect(createKeycloak as jest.Mock).toBeCalled() 72 | expect(initKeycloak as jest.Mock).toBeCalled() 73 | }) 74 | 75 | test('should call init with the default config', async () => { 76 | await vueKeycloak.install(appMock, { config: keycloakConfig }) 77 | 78 | expect(initKeycloak as jest.Mock).toBeCalledWith(defaultInitConfig) 79 | }) 80 | 81 | test('should call init config and extend the default config', async () => { 82 | await vueKeycloak.install(appMock, { 83 | config: keycloakConfig, 84 | initOptions: { 85 | flow: 'my-flow', 86 | }, 87 | }) 88 | 89 | expect(initKeycloak as jest.Mock).toBeCalledWith({ ...defaultInitConfig, flow: 'my-flow' }) 90 | }) 91 | }) 92 | -------------------------------------------------------------------------------- /src/keycloak.test.ts: -------------------------------------------------------------------------------- 1 | import { createKeycloak, getToken, initKeycloak, isTokenReady } from './keycloak' 2 | import Keycloak, { KeycloakConfig } from 'keycloak-js' 3 | import { hasFailed, isAuthenticated, isPending, setToken } from './state' 4 | import { defaultInitConfig } from './const' 5 | 6 | jest.mock('keycloak-js', () => jest.fn()) 7 | jest.mock('./state', () => { 8 | return { 9 | setToken: jest.fn(), 10 | hasFailed: jest.fn(), 11 | isPending: jest.fn(), 12 | isAuthenticated: jest.fn(), 13 | } 14 | }) 15 | 16 | describe('keyckoak', () => { 17 | const keycloakConfig: KeycloakConfig = { 18 | clientId: 'abc', 19 | realm: 'abc', 20 | url: 'abc', 21 | } 22 | 23 | const mockKeycloak = { 24 | token: 'abc', 25 | updateToken: jest.fn().mockImplementation(() => Promise.resolve()), 26 | init: jest.fn().mockImplementation(() => Promise.resolve(true)), 27 | } 28 | 29 | beforeEach(() => { 30 | ;(Keycloak as jest.Mock).mockClear() 31 | ;(setToken as jest.Mock).mockClear() 32 | ;(hasFailed as jest.Mock).mockClear() 33 | ;(isAuthenticated as jest.Mock).mockClear() 34 | ;(isPending as jest.Mock).mockClear() 35 | }) 36 | 37 | describe('isTokenReady', () => { 38 | test('should resolve', async () => { 39 | ;(Keycloak as jest.Mock).mockImplementation(() => ({ 40 | ...mockKeycloak, 41 | })) 42 | setTimeout(() => createKeycloak(keycloakConfig), 600) 43 | await isTokenReady() 44 | }) 45 | }) 46 | 47 | describe('getToken', () => { 48 | test('should return the new token', async () => { 49 | createKeycloak(keycloakConfig) 50 | const token = await getToken() 51 | 52 | expect(token).toBe('abc') 53 | }) 54 | 55 | test('should set hasFailed to true if it could not login', async () => { 56 | ;(Keycloak as jest.Mock).mockImplementation(() => ({ 57 | token: 'abc', 58 | updateToken: jest.fn().mockImplementation(() => Promise.reject()), 59 | })) 60 | 61 | createKeycloak(keycloakConfig) 62 | 63 | try { 64 | await getToken() 65 | } catch (error) { 66 | expect(hasFailed).toBeCalledWith(true) 67 | expect(error.message).toBe('Failed to refresh the token, or the session has expired') 68 | } 69 | }) 70 | }) 71 | 72 | describe('createKeycloak & getKeycloak', () => { 73 | test('should define a new keycloak instance and return it', () => { 74 | const result = createKeycloak(keycloakConfig) 75 | 76 | expect(result.token).toBe('abc') 77 | expect(Keycloak).toBeCalledWith(keycloakConfig) 78 | }) 79 | }) 80 | 81 | describe('initKeycloak', () => { 82 | test('should set isAuthenticated to true', async () => { 83 | ;(Keycloak as jest.Mock).mockImplementation(() => ({ 84 | ...mockKeycloak, 85 | })) 86 | 87 | createKeycloak(keycloakConfig) 88 | await initKeycloak(defaultInitConfig) 89 | 90 | expect(hasFailed).toBeCalledTimes(0) 91 | expect(isPending).toBeCalledTimes(2) 92 | expect(isPending).toBeCalledWith(false) 93 | expect(isAuthenticated).toBeCalledWith(true) 94 | }) 95 | 96 | test('should set isAuthenticated to false, due to login failure ', async () => { 97 | ;(Keycloak as jest.Mock).mockImplementation(() => ({ 98 | ...mockKeycloak, 99 | token: '', 100 | init: jest.fn().mockImplementation(() => Promise.resolve(false)), 101 | })) 102 | 103 | createKeycloak(keycloakConfig) 104 | await initKeycloak(defaultInitConfig) 105 | 106 | expect(hasFailed).toBeCalledTimes(0) 107 | expect(isPending).toBeCalledTimes(2) 108 | expect(isPending).toBeCalledWith(false) 109 | expect(isAuthenticated).toBeCalledWith(false) 110 | }) 111 | 112 | test('should set isAuthenticated to false, due to invalid token', async () => { 113 | ;(Keycloak as jest.Mock).mockImplementation(() => ({ 114 | ...mockKeycloak, 115 | token: '', 116 | init: jest.fn().mockImplementation(() => Promise.reject()), 117 | })) 118 | 119 | createKeycloak(keycloakConfig) 120 | try { 121 | await initKeycloak(defaultInitConfig) 122 | } catch (error) { 123 | expect(error.message).toBe('Could not read access token') 124 | expect(hasFailed).toBeCalledTimes(1) 125 | expect(isPending).toBeCalledTimes(2) 126 | expect(isPending).toBeCalledWith(false) 127 | expect(hasFailed).toBeCalledWith(true) 128 | expect(isAuthenticated).toBeCalledWith(false) 129 | } 130 | }) 131 | }) 132 | }) 133 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 | 3 | 6 | 9 | 12 | 13 |
4 | 5 | 7 |

+

8 |
10 | 11 |
14 | 15 | # vue-keycloak 16 | 17 | A small wrapper library for the [Keycloak JavaScript adapter](https://www.keycloak.org/docs/latest/securing_apps/#_javascript_adapter). 18 | 19 | > The library is made for [Vue 3.x.x](https://v3.vuejs.org/) and the [Composiotion API](https://v3.vuejs.org/api/composition-api.html). 20 | 21 | ## Instalation 22 | 23 | Install the [keycloak-js](https://www.keycloak.org/docs/latest/securing_apps/#_javascript_adapter) package , [jwt-decode](https://www.npmjs.com/package/jwt-decode) to decode the jwt token and our wrapper library with npm. 24 | 25 | ```bash 26 | npm install keycloak-js jwt-decode @baloise/vue-keycloak 27 | ``` 28 | 29 | ## Use plugin 30 | 31 | Import the library into your `src/main.ts` file or any other entry point. 32 | 33 | ```typescript 34 | import { vueKeycloak } from '@baloise/vue-keycloak' 35 | ``` 36 | 37 | Apply the library to the vue app instance. 38 | 39 | ```typescript 40 | const app = createApp(App) 41 | 42 | app.use(vueKeycloak, { 43 | initOptions: { 44 | flow: 'standard', // default 45 | checkLoginIframe: false, // default 46 | onLoad: 'login-required', // default 47 | } 48 | config: { 49 | url: 'http://keycloak-server/auth', 50 | realm: 'myrealm', 51 | clientId: 'myapp' 52 | } 53 | }) 54 | ``` 55 | 56 | Or use a JSON file with the configs. 57 | 58 | ```typescript 59 | app.use(vueKeycloak, '/keycloak.json') 60 | ``` 61 | 62 | ### Configuration 63 | 64 | | Config | Type | Description | 65 | | ----------- | ------------------------------ | ---------------------------------------- | 66 | | initOptions | `Keycloak.KeycloakInitOptions` | `initOptions` is Keycloak init options. | 67 | | config | `Keycloak.KeycloakConfig` | `config` are the Keycloak configuration. | 68 | 69 | Use the example below to generate dynamic Keycloak conifiguration. 70 | 71 | ```typescript 72 | app.use(vueKeycloak, async () => { 73 | return { 74 | config: { 75 | url: (await getAuthBaseUrl()) + '/auth', 76 | realm: 'myrealm', 77 | clientId: 'myapp', 78 | }, 79 | initOptions: { 80 | onLoad: 'check-sso', 81 | silentCheckSsoRedirectUri: window.location.origin + '/assets/silent-check-sso.html', 82 | }, 83 | } 84 | }) 85 | ``` 86 | 87 | > It is also possible to access the keycloak instance with `getKeycloak()` 88 | 89 | ## Use Token 90 | 91 | We export two helper functions for the token. 92 | 93 | ### getToken 94 | 95 | This function checks if the token is still valid and will update it if it is expired. 96 | 97 | > Have a look at our [vueAxios](https://github.com/baloise/vue-axios) plugin. 98 | 99 | ```typescript 100 | import { $axios } from '@baloise/vue-axios' 101 | import { getToken } from '@baloise/vue-keycloak' 102 | 103 | const axiosApiInstance = $axios.create() 104 | 105 | // Request interceptor for API calls 106 | axiosApiInstance.interceptors.request.use( 107 | async config => { 108 | const token = await getToken() 109 | config.headers = { 110 | Authorization: `Bearer ${token}`, 111 | } 112 | return config 113 | }, 114 | error => { 115 | Promise.reject(error) 116 | }, 117 | ) 118 | ``` 119 | 120 | ## Composition API 121 | 122 | ```typescript 123 | import { computed, defineComponent } from 'vue' 124 | import { useKeycloak } from '@baloise/vue-keycloak' 125 | 126 | export default defineComponent({ 127 | setup() { 128 | const { hasRoles, isPending } = useKeycloak() 129 | 130 | const hasAccess = computed(() => hasRoles(['RoleName'])) 131 | 132 | return { 133 | hasAccess, 134 | } 135 | }, 136 | }) 137 | ``` 138 | 139 | ### useKeycloak 140 | 141 | The `useKeycloak` function exposes the following reactive state. 142 | 143 | ```typescript 144 | import { useKeycloak } from '@baloise/vue-keycloak' 145 | 146 | const { 147 | isAuthenticated, 148 | isPending, 149 | hasFailed, 150 | token, 151 | decodedToken, 152 | username, 153 | roles, 154 | resourceRoles, 155 | keycloak, 156 | 157 | // Functions 158 | hasRoles, 159 | hasResourceRoles, 160 | } = useKeycloak() 161 | ``` 162 | 163 | | State | Type | Description | 164 | | --------------- | ------------------------------ | ------------------------------------------------------------------- | 165 | | isAuthenticated | `Ref` | If `true` the user is authenticated. | 166 | | isPending | `Ref` | If `true` the authentication request is still pending. | 167 | | hasFailed | `Ref` | If `true` authentication request has failed. | 168 | | token | `Ref` | `token` is the raw value of the JWT token. | 169 | | decodedToken | `Ref` | `decodedToken` is the decoded value of the JWT token. | 170 | | username | `Ref` | `username` the name of our user. | 171 | | roles | `Ref` | `roles` is a list of the users roles. | 172 | | resourceRoles | `Ref` | `resourceRoles` is a list of the users roles in specific resources. | 173 | | keycloak | `Keycloak.KeycloakInstance` | `keycloak` is the instance of the keycloak-js adapter. | 174 | 175 | #### Functions 176 | 177 | | Function | Type | Description | 178 | | ---------------- | ------------------------------------------------ | ---------------------------------------------------------------------------------- | 179 | | hasRoles | `(roles: string[]) => boolean` | `hasRoles` returns true if the user has all the given roles. | 180 | | hasResourceRoles | `(roles: string[], resource: string) => boolean` | `hasResourceRoles` returns true if the user has all the given roles in a resource. | 181 | 182 | # License 183 | 184 | Apache-2.0 Licensed | Copyright © 2021-present Gery Hirschfeld & Contributors 185 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright 2021 Baloise Group 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | --------------------------------------------------------------------------------