You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+314Lines changed: 314 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -23,6 +23,7 @@ It provides support for four passport based strategies.
23
23
3.[passport-local](https://github.com/jaredhanson/passport-local) - Passport strategy for authenticating with a username and password. This module lets you authenticate using a username and password in your Node.js applications.
24
24
4.[passport-oauth2-resource-owner-password](https://www.npmjs.com/package/passport-oauth2-resource-owner-password) - OAuth 2.0 resource owner password authentication strategy for Passport. This module lets you authenticate requests containing resource owner credentials in the request body, as [defined](http://tools.ietf.org/html/draft-ietf-oauth-v2-27#section-1.3.3) by the OAuth 2.0 specification.
25
25
5.[passport-google-oauth2](https://github.com/jaredhanson/passport-google-oauth2) - Passport strategy for authenticating with Google using the Google OAuth 2.0 API. This module lets you authenticate using Google in your Node.js applications.
26
+
6.[keycloak-passport](https://github.com/exlinc/keycloak-passport) - Passport strategy for authenticating with Keycloak. This library offers a production-ready and maintained Keycloak Passport connector.
26
27
27
28
You can use one or more strategies of the above in your application. For each of the strategy (only which you use), you just need to provide your own verifier function, making it easily configurable. Rest of the strategy implementation intricacies is handled by extension.
28
29
@@ -1083,6 +1084,319 @@ For accessing the authenticated AuthUser model reference, you can inject the CUR
1083
1084
privatereadonlygetCurrentUser: Getter<User>,
1084
1085
```
1085
1086
1087
+
### Keycloak
1088
+
1089
+
First, create a AuthUser model implementing the IAuthUser interface. You can implement the interface in the user model itself. See sample below.
1090
+
1091
+
```ts
1092
+
@model({
1093
+
name: 'users',
1094
+
})
1095
+
exportclassUserextendsEntityimplementsIAuthUser {
1096
+
@property({
1097
+
type: 'number',
1098
+
id: true,
1099
+
})
1100
+
id?:number;
1101
+
1102
+
@property({
1103
+
type: 'string',
1104
+
required: true,
1105
+
name: 'first_name',
1106
+
})
1107
+
firstName:string;
1108
+
1109
+
@property({
1110
+
type: 'string',
1111
+
name: 'last_name',
1112
+
})
1113
+
lastName:string;
1114
+
1115
+
@property({
1116
+
type: 'string',
1117
+
name: 'middle_name',
1118
+
})
1119
+
middleName?:string;
1120
+
1121
+
@property({
1122
+
type: 'string',
1123
+
required: true,
1124
+
})
1125
+
username:string;
1126
+
1127
+
@property({
1128
+
type: 'string',
1129
+
})
1130
+
email?:string;
1131
+
1132
+
// Auth provider - 'keycloak'
1133
+
@property({
1134
+
type: 'string',
1135
+
required: true,
1136
+
name: 'auth_provider',
1137
+
})
1138
+
authProvider:string;
1139
+
1140
+
// Id from external provider
1141
+
@property({
1142
+
type: 'string',
1143
+
name: 'auth_id',
1144
+
})
1145
+
authId?:string;
1146
+
1147
+
@property({
1148
+
type: 'string',
1149
+
name: 'auth_token',
1150
+
})
1151
+
authToken?:string;
1152
+
1153
+
@property({
1154
+
type: 'string',
1155
+
})
1156
+
password?:string;
1157
+
1158
+
constructor(data?:Partial<User>) {
1159
+
super(data);
1160
+
}
1161
+
}
1162
+
```
1163
+
1164
+
Create CRUD repository for the above model. Use loopback CLI.
1165
+
1166
+
```sh
1167
+
lb4 repository
1168
+
```
1169
+
1170
+
Add the verifier function for the strategy. You need to create a provider for the same. You can add your application specific business logic for client auth here. Here is a simple example.
throw new HttpErrors.Unauthorized(AuthErrorKeys.InvalidCredentials);
1387
+
}
1388
+
}
1389
+
```
1390
+
1391
+
Please note above that we are creating two new APIs for keycloak auth. The first one is for UI clients to hit. We are authenticating client as well, then passing the details to the keycloak auth. Then, the actual authentication is done by keycloak authorization url, which redirects to the second API we created after success. The first API method body is empty as we do not need to handle its response. The keycloak auth provider in this package will do the redirection for you automatically.
1392
+
1393
+
For accessing the authenticated AuthUser model reference, you can inject the CURRENT_USER provider, provided by the extension, which is populated by the auth action sequence above.
If you've noticed a bug or have a question or have a feature request, [search the issue tracker](https://github.com/sourcefuse/loopback4-authentication/issues) to see if someone else in the community has already created a ticket.
0 commit comments