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
* feat(component): add a new strategy for otp
added a new 2-factor authentication strategy
GH-69
* feat(component): add a new strategy for otp
added a new 2-factor authentication strategy
GH-69
* feat(component): add a new strategy for otp
added a new 2-factor authentication strategy.
gh-69
Copy file name to clipboardExpand all lines: README.md
+193
Original file line number
Diff line number
Diff line change
@@ -25,6 +25,7 @@ It provides support for seven passport based strategies.
25
25
7.[passport-instagram](https://github.com/jaredhanson/passport-instagram) - Passport strategy for authenticating with Instagram using the Instagram OAuth 2.0 API. This module lets you authenticate using Instagram in your Node.js applications.
26
26
8.[passport-apple](https://github.com/ananay/passport-apple) - Passport strategy for authenticating with Apple using the Apple OAuth 2.0 API. This module lets you authenticate using Apple in your Node.js applications.
27
27
9.[passport-facebook](https://github.com/jaredhanson/passport-facebook) - Passport strategy for authenticating with Facebook using the Facebook OAuth 2.0 API. This module lets you authenticate using Facebook in your Node.js applications.
28
+
10. custom-passport-otp - Created a Custom Passport strategy for 2-Factor-Authentication using OTP (One Time Password).
28
29
29
30
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.
30
31
@@ -793,6 +794,198 @@ For accessing the authenticated AuthUser and AuthClient model reference, you can
First, create a OtpCache model. This model should have OTP and few details of user and client (which will be used to retrieve them from database), it will be used to verify otp and get user, client. See sample below.
800
+
801
+
```ts
802
+
@model()
803
+
exportclassOtpCacheextendsEntity {
804
+
@property({
805
+
type: 'string',
806
+
})
807
+
otp:string;
808
+
809
+
@property({
810
+
type: 'string',
811
+
})
812
+
userId:string;
813
+
814
+
@property({
815
+
type: 'string',
816
+
})
817
+
clientId:string;
818
+
819
+
@property({
820
+
type: 'string',
821
+
})
822
+
clientSecret:string;
823
+
824
+
constructor(data?:Partial<OtpCache>) {
825
+
super(data);
826
+
}
827
+
}
828
+
```
829
+
830
+
Create [redis-repository](https://loopback.io/doc/en/lb4/Repository.html#define-a-keyvaluerepository) for the above model. Use loopback CLI.
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 auth here. Here is a simple example.
Then, you need to create APIs, where you will first authenticate the user, and then send the OTP to user's email/phone. See below.
925
+
926
+
```ts
927
+
//You can use your other strategies also
928
+
@authenticate(STRATEGY.LOCAL)
929
+
@post('/auth/send-otp', {
930
+
responses: {
931
+
[STATUS_CODE.OK]: {
932
+
description: 'Send Otp',
933
+
content: {
934
+
[CONTENT_TYPE.JSON]: Object,
935
+
},
936
+
},
937
+
},
938
+
})
939
+
asynclogin(
940
+
@requestBody()
941
+
req: LoginRequest,
942
+
): Promise<{
943
+
key:string;
944
+
}> {
945
+
946
+
// User is authenticated before this step.
947
+
// Now follow these steps:
948
+
// 1. Create a unique key.
949
+
// 2. Generate and send OTP to user's email/phone.
950
+
// 3. Store the details in redis-cache using key created in step-1. (Refer OtpCache model mentioned above)
951
+
// 4. Response will be the key created in step-1
952
+
}
953
+
```
954
+
955
+
After this, create an API with @@authenticate(STRATEGY.OTP) decorator. See below.
956
+
957
+
```ts
958
+
@authenticate(STRATEGY.OTP)
959
+
@post('/auth/login-otp', {
960
+
responses: {
961
+
[STATUS_CODE.OK]: {
962
+
description: 'Auth Code',
963
+
content: {
964
+
[CONTENT_TYPE.JSON]: Object,
965
+
},
966
+
},
967
+
},
968
+
})
969
+
asynclogin(
970
+
@requestBody()
971
+
req: {
972
+
key: 'string';
973
+
otp: 'string';
974
+
},
975
+
): Promise<{
976
+
code:string;
977
+
}> {
978
+
......
979
+
}
980
+
```
981
+
982
+
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.
0 commit comments