Skip to content

feat: Add SMS Auth - #1362

Closed
IzenDev wants to merge 39 commits into
serverpod:mainfrom
IzenDev:main
Closed

IzenDev wants to merge 39 commits into
serverpod:mainfrom
IzenDev:main

Conversation

@IzenDev

@IzenDev IzenDev commented Sep 13, 2023 •

Copy link
Copy Markdown

This adds the built-in feature for SMS Auth with the new package serverpod_auth_sms_flutter, which allows the client to authenticate users with SMS and with their favorite SMS provider without worrying about the Verification code being stored in the database.

Added SMS Auth to authenticate users with phones.

Pre-launch Checklist

  • I read the Contribute page and followed the process outlined there for submitting PRs.
  • This update contains only one single feature or bug fix and nothing else. (If you are submitting multiple fixes, please make multiple PRs.)
  • I read and followed the Dart Style Guide and formatted the code with dart format.
  • I listed at least one issue that this PR fixes in the description above.
  • I updated/added relevant documentation (doc comments with ///), and made sure that the documentation follows the same style as other Serverpod documentation. I checked spelling and grammar.
  • I added new tests to check the change I am making.
  • All existing and new tests are passing.
  • Any breaking changes are documented below.

If you need help, consider asking for advice on the discussion board.

Breaking changes

None

@github-actions

github-actions Bot commented Sep 13, 2023 •

Copy link
Copy Markdown

CLA Assistant Lite bot All contributors have signed the CLA ✍️ ✅

@IzenDev

IzenDev commented Sep 13, 2023

Copy link
Copy Markdown
Author

I have read the CLA Document and I hereby sign the CLA

@IzenDev

IzenDev commented Sep 15, 2023

Copy link
Copy Markdown
Author

The test failed after I modified serverpod/modules/serverpod_auth/serverpod_auth_server/lib/src/protocol/user_info.yaml. I added phoneNumber as a Steing?.
Can someone explain what could be done to fix it?

@Isakdl

Isakdl commented Sep 15, 2023 •

Copy link
Copy Markdown
Collaborator

Could you try to run serverpod migrate inside the test project? You have to use the cli from main for this to work (How to?).

Since you have changed the database definitions it might simply be an issue with the database state that is out of sync.

@Isakdl

Isakdl commented Sep 15, 2023

Copy link
Copy Markdown
Collaborator

You can also run these test locally by executing the script util/run_tests_single_server from the root of the entire mono-repo. You need docker for this to work.

@IzenDev

IzenDev commented Sep 15, 2023

Copy link
Copy Markdown
Author

@Isakdl All the tests passed locally. Thanks.

@IzenDev IzenDev changed the title Implemented SMS Auth Feat: Implemented SMS Auth Sep 15, 2023
@IzenDev IzenDev changed the title Feat: Implemented SMS Auth feat: Implemented SMS Auth Sep 15, 2023
@IzenDev IzenDev changed the title feat: Implemented SMS Auth Feat: Add SMS Auth Sep 15, 2023
@IzenDev IzenDev changed the title Feat: Add SMS Auth feat: Add SMS Auth Sep 16, 2023

@SandPod SandPod left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is really awesome work and I look forward to getting this into Serverpod. It is a highly requested feature and will be very welcome by the community.

This is a first review on the backend code.

Once the comments are fixed, please rebase on latest main and I will review the frontend part.

Tests will be important for getting this merged.

table: serverpod_sms_auth
fields:
### The id of the user, corresponds to the id field in the [UserInfo].
userId: int

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Question: Would we benefit from using a one-to-one relation here instead? https://docs.serverpod.dev/concepts/database/relations/one-to-one

That would enable us to have the record automatically removed when the user entry is removed.

if (await _hasTooManyFailedSignIns(session, phoneNumber)) {
return AuthenticationResponse(
success: false,
failReason: AuthenticationFailReason.tooManyFailedAttempts);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion (NIT): Add comma to improve formatting.

Suggested change
failReason: AuthenticationFailReason.tooManyFailedAttempts);
failReason: AuthenticationFailReason.tooManyFailedAttempts,
);

import 'package:serverpod_auth_server/src/business/sms_auth.dart';

/// Endpoint for handling Sign in with SMS.
class SmsEndpoint extends Endpoint {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion: Move all business logic from Endpoint to make it testable without a client.

The more we can isolate what we test the better feedback future developers will get when they try and change the code. Moving the required tests from e2e tests to integration tests enables us to better control the environment and hopefully provide better feedback to future developers.

Comment on lines +107 to +108
var dataToHash = '$phoneNumber$otp$secretHashKey';
var hmac = Hmac(sha256, utf8.encode(secretHashKey));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion: Remove secretHashKey from dataToHash.

It is enough to use the secretHashKey as key for the Hmac encryption.

Comment on lines +103 to +104
var secretHashKey =
Serverpod.instance.getPassword('SMSHash') ?? 'serverpod SMS hash';

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion: Remove the default value and throw if no value is set.

Having default hash key values is not recommended, users will probably forget to set it and end up having easily guessable OTP hashes. Better to require users to set their own hash key.

We are aiming to change this for the other authentication methods as well.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion: Enforce min key length based on best practice for Hmac-sha256.

As far as I can read the recommended key lenght is at least 64 bytes. We should help our users make good decisions there and enforce a minimum of 64 bytes when configuring the key.

);

if (entry == null) {
session.log(' - no entry found', level: LogLevel.debug);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion: change logging message to creating new user.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, harmonize this log message with the next one. This seems to give a strange flow.

Comment on lines +84 to +91
// Create user if it doesn't exist and return it
if (userInfo == null) {
session.log('- user not fount', level: LogLevel.debug);
return AuthenticationResponse(
success: false,
failReason: AuthenticationFailReason.invalidCredentials,
);
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Question: The comment above doesn't seem to match what this code does.

Could we avoid multiple lookups for the user in the database? Maybe we could return it as a record from the SMSs.createUser method?

@@ -0,0 +1,63 @@
# This file is generated. Do not modify, instead edit the files in the templates/pubspecs directory.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion: This pubspec needs to be reflected in the templates folder.

Please check the other flutter pubspecs in the template folder.

@@ -0,0 +1,4 @@
include: package:flutter_lints/flutter.yaml

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion: Change to include: package:serverpod_lints/public_flutter.yaml

@@ -0,0 +1 @@
//Todo: Write test

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Chore: This code needs tests.

I'm working on a PR now that might help set the expectation around what tests are required: #1993

@SandPod SandPod left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please see comments in the previous post.

@SandPod SandPod mentioned this pull request Mar 25, 2024
@shoomban

Copy link
Copy Markdown

Can u help me to get the sms of +971543566766 and
+919633659993

@SandPod

SandPod commented May 22, 2025

Copy link
Copy Markdown
Contributor

Closing this as stale.

This is connected to issue #2050 and can be used as refeference once this is picked up.

Since we are working on the new authentication module. This will be added as a provider there instead.

@SandPod SandPod closed this May 22, 2025
@github-actions github-actions Bot locked and limited conversation to collaborators May 22, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants