@samhammer/authentication-vue 2.5.2 → 2.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +172 -0
  3. package/package.json +4 -4
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2022 Samhammer AG
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,172 @@
1
+ # Samhammer.Authentication.Vue
2
+
3
+ This is a generic Oauth/Jwt authentication library for vuejs applications.
4
+ It can be used with any vue app if you are relying on keycloak for authentication.
5
+
6
+ Uses keycloak-js internally: https://www.npmjs.com/package/keycloak-js
7
+
8
+ Release on npm: https://www.npmjs.com/package/@samhammer/authentication-vue
9
+
10
+ ## What can it do
11
+
12
+ - Supports keycloak login
13
+ - Guest auth / User identification over a random guid
14
+ - Automatically add the token to requests
15
+ - Handles token refresh
16
+ - Logout
17
+
18
+ ## How to use
19
+
20
+ ### Initialization
21
+
22
+ Add the import: import Auth from '@samhammer/authentication-vue';
23
+
24
+ Before checking if the user is authenticated or doing anything else with the library you have to ensure that the initialization has been done properly. You can call the below method multiple times and the initialization is just done once during application lifetime.
25
+
26
+ ```js
27
+ import Auth from "@samhammer/authentication-vue";
28
+ Auth.initOnce(authOptions);
29
+ ```
30
+
31
+ ##### InitOnce Arguments:
32
+
33
+ | Type | Name | Description |
34
+ | ----------- | ------------------- | -------------------------------------------------------------------- |
35
+ | `guestAuth` | guestClientId | The store prefix used for guest login |
36
+ | `guestAuth` | guestRoles | The assigned roles when using guest login |
37
+ | `userAuth` | authUrl | The base auth url of keycloak (e.g. "https://auth.myserver.de/auth") |
38
+ | `userAuth` | realm | Authentication realm used in keycloak |
39
+ | `userAuth` | apiClientId | The client id your api uses (required for role checks) |
40
+ | `userAuth` | appClientId | The id of the public client used for authentication |
41
+ | `optional` | keycloakInitOptions | Object to allow overriding of all settings on keycloakjs.init(). |
42
+ | `optional` | store | Instance of a store. Default: LocalStore (saves in LocalStorage) |
43
+
44
+ Note:
45
+
46
+ - `guestAuth` This has to be set when using guest auth
47
+ - `userAuth` This has to be set when using user auth
48
+ - `optional` This field can be set if it is required
49
+
50
+ ##### InitOnce Return value and Events
51
+
52
+ InitOnce needs to be called before any login action and also to initialize authentication state.
53
+ In case we are already authentication, the event "AuthEventNames.isAlreadyAuthenticated" is emitted.
54
+
55
+ ### Keycloak login
56
+
57
+ Below call will trigger the web login flow. The auth token is saved to the store.
58
+ After successful login keycloak will redirect to the given url.
59
+
60
+ ```js
61
+ import Auth from "@samhammer/authentication-vue";
62
+ Auth.login("idp");
63
+ ```
64
+
65
+ ### Guest login
66
+
67
+ If you wan't to use guest authentication just call "Auth.loginGuest()". A new random guid will be generated and saved in the store.
68
+
69
+ Guests always have the role "User" only.
70
+
71
+ ```js
72
+ import Auth from "@samhammer/authentication-vue";
73
+ Auth.loginGuest();
74
+ ```
75
+
76
+ ### Logout
77
+
78
+ Logout removes the token from the store and does a keycloak logout (if not guest).
79
+
80
+ ```js
81
+ import Auth from "@samhammer/authentication-vue";
82
+ Auth.logout();
83
+ ```
84
+
85
+ ### Check auth state and roles
86
+
87
+ - Auth.authenticated => Check if a user is authenticated. Returns true if so.
88
+ - Auth.hasRole('roleName', 'apiClientId') => Check if a user has a specific role for api client id, if apiclientid is not specified, the apiclientid in AuthOptions will be used.
89
+ - Auth.isGuest => Returns true if authenticted as guest
90
+
91
+ ### Send auth token to api
92
+
93
+ ##### With axios
94
+
95
+ - addAuthTokenInterceptor => Automatically adds the auth token to axios requests. With keycloak authentication the header named "Authentication" is used for the json web token. With guest auth the random guid is added to the header "guestid".
96
+ - addAuthErrorInterceptor => Handles auth errors by emititing the vue event "AuthEventNames.loginRequired" in case of an error 401 and "AuthEventNames.permissionDenied" in case of an 403 http status response
97
+
98
+ Just add the following snippet for the described behavior:
99
+
100
+ ```js
101
+ import axios, { AxiosInstance } from "axios";
102
+ import { AuthAxiosInterceptor } from "@samhammer/authentication-vue";
103
+
104
+ AuthAxiosInterceptor.addAuthTokenInterceptor(axios);
105
+ AuthAxiosInterceptor.addAuthErrorInterceptor(axios);
106
+ ```
107
+
108
+ ##### Manually
109
+
110
+ You can implement passing the token by yourself. Just don't call the methods to add axios interceptors and fetch the token like that:
111
+
112
+ ```js
113
+ import Auth from "@samhammer/authentication-vue";
114
+ Auth.getToken();
115
+ ```
116
+
117
+ Note: In case of guest authentication the token is the random guid of the guest.
118
+
119
+ ### Events
120
+
121
+ - isAlreadyAuthenticated = Triggered on initOnce if the user is already signed in
122
+ - loginRequired = Triggered on an axios request in case of a status code 401 (addAuthErrorInterceptor required)
123
+ - permissionDenied = Triggered on an axios request in case of a status code 403 (addAuthErrorInterceptor required)
124
+
125
+ Can be handled like that:
126
+
127
+ ```js
128
+ import { AuthEvents, AuthEventNames } from "@samhammer/authentication-vue";
129
+
130
+ public mounted(): void {
131
+ AuthEvents.on(AuthEventNames.permissionDenied, this.onPermissionDenied);
132
+ }
133
+
134
+ public beforeDestroy(): void {
135
+ AuthEvents.off(AuthEventNames.permissionDenied, this.onPermissionDenied);
136
+ }
137
+
138
+ private onPermissionDenied(): void {
139
+ // Do something
140
+ }
141
+ ```
142
+
143
+ ### Store
144
+
145
+ Defines where data such as the refresh, access and id token are stored. There are two predefined stores: the LocalStore, which stores the data in the local storage, and the ChromeStore, which stores the data in the local chrome storage. The second can be used for Chrome extensions, for example.
146
+
147
+ ##### Define own store
148
+
149
+ Create a class and implement the provided Store interface:
150
+
151
+ ```js
152
+ import { Store } from "@samhammer/authentication-vue";
153
+
154
+ class CustomStore implements Store {
155
+ setItem(key: string, value: string): Promise<void> {
156
+ // Save data
157
+ }
158
+ getItem(key: string): Promise<string> {
159
+ // Save data
160
+ }
161
+ removeItem(key: string): Promise<void> {
162
+ // Delete data
163
+ }
164
+ }
165
+ ```
166
+
167
+ After that add the store in the authOptions.
168
+
169
+ ## How to publish
170
+ - Create & Push a tag with new version number
171
+ - The CICD actions will take this version number for npm package automatically
172
+ - Check github action to validated, that package was released to npm registry.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@samhammer/authentication-vue",
3
- "version": "2.5.2",
3
+ "version": "2.6.0",
4
4
  "description": "Keycloak authentication for VueJS projects",
5
5
  "main": "lib/index.js",
6
6
  "typings": "lib/index.d.ts",
@@ -23,7 +23,7 @@
23
23
  "@rushstack/eslint-patch": "^1.2.0",
24
24
  "@types/chrome": "^0.0.251",
25
25
  "@types/lodash": "^4.14.191",
26
- "@types/node": "^20.19.25",
26
+ "@types/node": "^24.12.2",
27
27
  "@types/uuid": "^9.0.1",
28
28
  "@typescript-eslint/eslint-plugin": "^5.54.1",
29
29
  "@typescript-eslint/parser": "^5.54.1",
@@ -42,7 +42,7 @@
42
42
  "uuid": "^9.0.0"
43
43
  },
44
44
  "volta": {
45
- "node": "18.19.0",
46
- "yarn": "1.22.21"
45
+ "node": "24.15.0",
46
+ "yarn": "4.14.1"
47
47
  }
48
48
  }