adventurous-addition-28416
04/19/2025, 3:38 AMbrief-egg-61061
04/19/2025, 3:38 AMbrief-egg-61061
04/19/2025, 3:39 AMyaml selfservice: methods: code: passwordless_enabled: true
You can do this through the [Ory Console UI](https://console.ory.sh/projects/current/passwordless) or using the Ory CLI: ory patch identity-config <your-project-id> \ --add '/selfservice/methods/code/passwordless_enabled=true'
[Enable SMS one-time-code (OTP)](https://www.ory.sh/docs/kratos/passwordless/one-time-code#enable-sms-one-time-code-otp) ### 2. Configure your identity schema Ensure your identity schema has a phone number field marked as an identifier for the code method: json { "<http://ory.sh/kratos|ory.sh/kratos>": { "credentials": { "code": { "identifier": true, "via": "sms" } } } }
[Passwordless one-time code login identifier](https://www.ory.sh/docs/kratos/manage-identities/customize-identity-schema#passwordless-one-time-code-login-identifier) ### 3. Configure SMS delivery Set up an SMS channel in your Ory configuration: yaml courier: channels: - id: sms type: http request_config: url: <https://api.twilio.com/2010-04-01/Accounts/AXXXXXXXXXXXXXX/Messages.json> method: POST body: <base64://ZnVuY3Rpb24oY3R4KSB7CiAgVG86IGN0eC5yZWNpcGllbnQsCiAgQm9keTogY3R4LmJvZHksCn0>= headers: Content-Type: application/x-www-form-urlencoded auth: type: basic_auth config: user: AXXXXXXX password: XXXX
[Send SMS to your users](https://www.ory.sh/docs/kratos/emails-sms/sending-sms) ### 4. Start the login flow To start the login flow, direct users to: https://<your-custom-domain-or-project-slug>/self-service/login/browser
The user will be prompted to enter their phone number, and then a one-time code will be sent to that number via SMS. [User Login](https://www.ory.sh/docs/kratos/self-service/flows/user-login) ### Important security note Be aware that SMS OTP is considered less secure due to vulnerabilities like SIM swapping, SMS interception, and phishing. Many security experts and government agencies no longer recommend it for sensitive applications. Consider using more secure methods like Passkeys if possible. [SMS one-time-code security warning](https://www.ory.sh/docs/kratos/passwordless/one-time-code#enable-sms-one-time-code-otp)adventurous-addition-28416
04/19/2025, 3:41 AMbrief-egg-61061
04/19/2025, 3:41 AMbrief-egg-61061
04/19/2025, 3:41 AMhttps://<your-project-slug>.<http://projects.oryapis.com/self-service/login/api|projects.oryapis.com/self-service/login/api>
This will return a JSON response containing a flow
object with an id
that you'll need for the next step. ### 2. Submit the Login Flow with Phone Number Create another request to submit the phone number: - Method: POST - URL: https://<your-project-slug>.<http://projects.oryapis.com/self-service/login|projects.oryapis.com/self-service/login>
- Headers: - Content-Type: application/json - Body: json { "method": "code", "identifier": "+1234567890", // Replace with the actual phone number "flow": "flow-id-from-previous-step" }
### 3. Submit the OTP Code After receiving the SMS with the code, create a final request to submit the code: - Method: POST - URL: https://<your-project-slug>.<http://projects.oryapis.com/self-service/login|projects.oryapis.com/self-service/login>
- Headers: - Content-Type: application/json - Body: json { "method": "code", "code": "123456", // Replace with the actual code received via SMS "csrf_token": "csrf-token-from-flow", // If required "flow": "flow-id-from-previous-step" }
The API will respond with a session token or cookie that you can use for authenticated requests. Note that the exact request format may vary slightly depending on your specific Ory configuration. You should refer to the [Ory API Reference](https://www.ory.sh/docs/reference/api) for the most up-to-date API specifications. Remember that SMS OTP is considered less secure than other authentication methods due to vulnerabilities like SIM swapping and SMS interception.adventurous-addition-28416
04/19/2025, 3:45 AMbrief-egg-61061
04/19/2025, 3:45 AMbrief-egg-61061
04/19/2025, 3:45 AMjson "phone": { "type": "string", "title": "Phone number", "format": "tel", "<http://ory.sh/kratos|ory.sh/kratos>": { "credentials": { "code": { "identifier": true, "via": "sms" } } } }
2. Verify your SMS provider configuration: Ensure your SMS channel is properly configured in the Ory configuration: yaml courier: channels: - id: sms type: http request_config: url: "<https://your-sms-provider-url>" method: "POST" # Other configuration...
3. Check logs: Look for any SMS-related logs in your Kratos logs. There should be entries like: level=info msg=[DEBUG] POST <https://your-sms-provider-url> level=debug msg=Courier sent out mailer. http_server=<https://your-sms-provider-url> message_type=sms
4. Temporary workaround: As mentioned in the GitHub issue, you could try disabling email verification in your schema if you only need SMS verification: diff "email": { "type": "string", "format": "email", "title": "E-mail", "<http://ory.sh/kratos|ory.sh/kratos>": { "credentials": { "password": { "identifier": true } }, - "verification": { - "via": "email" - } } }
This appears to be a bug in Ory Kratos where both verification methods are triggered regardless of which one you're using for login. Since this is an open issue, you might want to follow the GitHub issue for updates or consider contributing a fix if possible. Let me know if you need more specific help with your configuration!adventurous-addition-28416
04/19/2025, 3:48 AMbrief-egg-61061
04/19/2025, 3:48 AMbrief-egg-61061
04/19/2025, 3:48 AM{"address":[{"To":"<tel:+5511986381688|+5511986381688>","Via":"sms"}],"audience":"application","level":"debug","msg":"Preparing login code"...
But then it's sending it via email instead of SMS: {"audience":"audit","level":"info","login_code":"731384","login_code_id":"ba405da9-aecd-459a-8e41-9a10e633bd83","login_flow_id":"2a7280dd-b730-41a3-b59a-d92bafca4edc","msg":"Sending out login email with code."...
And the request ultimately fails with a 400 Bad Request status. ## Analysis There are a few potential issues: 1. Courier Configuration: Your SMS channel configuration looks mostly correct, but there are a couple of issues: - You're using PUT
method instead of the more common POST
for SMS APIs - The base64 body appears to be truncated in your configuration (it has a <
character in the middle) 2. Identity Schema: Your schema looks correct for SMS verification, with: json "code": { "identifier": true, "via": "sms" }
and json "verification": { "via": "sms" }
3. Message Routing: Despite configuring SMS, the log shows "Sending out login email with code" which suggests Kratos is trying to send via email instead of SMS. ## Possible Solutions 1. Fix the courier configuration: - Change the method from PUT
to POST
- Make sure the base64 body is correct without any truncation or extra characters - Example of a correct body (decoded): function(ctx) { To: ctx.recipient, Body: ctx.body, }
2. Check SMS templates: Make sure you have the proper SMS templates configured: yaml courier: templates: login_code: valid: sms: body: plaintext: "<base64://WW91ciBsb2dpbiBjb2RlIGlzOiB7eyAuTG9naW5Db2RlIH19>"
3. Verify webhook endpoint: - Test your webhook.site endpoint separately to ensure it's accepting requests - Check if it requires any specific authentication or headers 4. Check for email configuration: - Since the logs show "Sending out login email with code", make sure you don't have conflicting email configurations This issue appears similar to the one described in [GitHub issue #4161](https://github.com/ory/kratos/issues/4161), where there's confusion between email and SMS verification channels. The key difference is that in your case, it's trying to send an email even though you've only configured SMS verification in your schema. Let me know if you need more specific guidance on any of these potential solutions!adventurous-addition-28416
04/19/2025, 3:53 AMbrief-egg-61061
04/19/2025, 3:53 AMbrief-egg-61061
04/19/2025, 3:53 AMyaml courier: templates: login_code: valid: sms: body: plaintext: "<base64://WW91ciBsb2dpbiBjb2RlIGlzOiB7eyAuTG9naW5Db2RlIH19>"
2. Verify template paths: Self-hosted Kratos expects templates at specific paths: <kratos-root>/<template-root>/verification/valid <kratos-root>/<template-root>/verification/invalid
3. Check template override path: Make sure your configuration includes the correct template override path: yaml courier: template_override_path: /conf/courier-template
4. Verify SMS channel configuration: Ensure your SMS channel is properly configured with the correct ID (sms
): yaml courier: channels: - id: sms type: http request_config: # Your configuration
The issue you're experiencing is similar to what's described in [GitHub issue #4161](https://github.com/ory/kratos/issues/4161), where there's confusion between email and SMS verification channels. This issue might be more pronounced in self-hosted environments where configuration is more manual compared to the Ory Network. If you continue to face issues, you might want to check your Kratos logs for more detailed error messages or consider using the Ory CLI to export a working configuration from your Ory Network project and apply it to your self-hosted instance.adventurous-addition-28416
04/19/2025, 3:59 AMbrief-egg-61061
04/19/2025, 3:59 AMbrief-egg-61061
04/19/2025, 4:00 AMcourier.templates.login_code.valid.email: <nil> ^-- one or more required properties are missing
This means that while you've correctly configured the SMS template, you're missing the required email template configuration. ## Solution You need to add the email template configuration alongside your SMS template. Here's how to fix your configuration: yaml templates: login_code: valid: sms: body: plaintext: "<base64://WW91ciBsb2dpbiBjb2RlIGlzOiB7eyAuTG9naW5Db2RlIH19>" email: # Add this section body: html: "<base64://PGh0bWw+WW91ciBsb2dpbiBjb2RlIGlzOiB7eyAuTG9naW5Db2RlIH19PC9odG1sPg>==" plaintext: "<base64://WW91ciBsb2dpbiBjb2RlIGlzOiB7eyAuTG9naW5Db2RlIH19>" subject: "<base64://WW91ciBsb2dpbiBjb2Rl>"
This is a known requirement in self-hosted Ory Kratos - you need to define both email and SMS templates even if you're only using one of them. The Ory Network handles this automatically, which is why it works there without this explicit configuration. The base64 strings in the example decode to: - plaintext: "Your login code is: {{ .LoginCode }}" - html: "<html>Your login code is: {{ .LoginCode }}</html>" - subject: "Your login code" You can encode your own custom templates using a base64 encoder. [Email templates in self-hosted Ory Kratos](https://www.ory.sh/docs/kratos/self-hosted/mail-courier-templates) explains that in self-hosted environments, you need to be more explicit with template configurations compared to Ory Network.adventurous-addition-28416
04/19/2025, 4:08 AMbrief-egg-61061
04/19/2025, 4:08 AMbrief-egg-61061
04/19/2025, 4:09 AMsession
hook might be interfering with your webhook execution. According to the troubleshooting documentation: > If an Ory Action webhook fails to trigger, verify whether the sign in after registration session
hook is activated. Because the session
hook modifies the HTTP response, no other hooks are executed after it. [Troubleshoot Ory Actions Webhook issues](https://www.ory.sh/docs/troubleshooting/troubleshoot-actions) Check your configuration to ensure that if you have a session
hook, it's placed after any webhook you want to execute. ## 2. Webhook Order in Configuration The order of hooks matters. In your configuration, make sure your webhook comes before the session hook: yaml hooks: - hook: web_hook config: url: <https://webhook.site/0757f9a2-106a-4938-bb50-9e8ec65177d3> # other webhook config - hook: session
## 3. SMS Channel Configuration Your SMS channel configuration might have issues: yaml courier: channels: - id: sms request_config: body: <base64://ZnVuY3Rpb24oY3R4KSB7CiAgVG86IGN0eC5yZWNpcGllbnQsCiAgQm9keTogY3R>4LmJvZHksCn0= method: PUT url: <https://webhook.site/0757f9a2-106a-4938-bb50-9e8ec65177d3> type: http
The base64 string appears to be malformed (it has a <
character at the beginning and a >
character in the middle). Make sure your base64 encoding is correct. ## 4. Method-Specific Hooks Make sure you're configuring the hooks specifically for the code
method: yaml selfservice: flows: login: after: code: hooks: - hook: web_hook config: url: <https://webhook.site/0757f9a2-106a-4938-bb50-9e8ec65177d3> # other webhook config - hook: session
## 5. Debug with Logs Enable debug logging to see if there are any errors when Kratos tries to send the SMS: yaml log: level: debug
Look for logs related to the courier service and SMS sending. ## 6. Test Your Webhook Endpoint Make sure your webhook.site endpoint is accessible and working correctly by testing it with a simple HTTP request from another tool. If you continue to face issues, you might want to try a different SMS provider configuration or consider using the Ory CLI to export a working configuration from your Ory Network project to compare with your self-hosted setup. Let me know if any of these suggestions help or if you need further assistance!adventurous-addition-28416
04/19/2025, 4:11 AMbrief-egg-61061
04/19/2025, 4:11 AMbrief-egg-61061
04/19/2025, 4:12 AMyaml body: <base64://ZnVuY3Rpb24oY3R4KSB7CiAgVG86IGN0eC5yZWNpcGllbnQsCiAgQm9keTogY3R>4LmJvZHksCn0= url: <https://webhook.site/0757f9a2-106a-4938-bb50-9e8ec65177d3>
Remove the angle brackets: yaml body: <base64://ZnVuY3Rpb24oY3R4KSB7CiAgVG86IGN0eC5yZWNpcGllbnQsCiAgQm9keTogY3R4LmJvZHksCn0>= url: <https://webhook.site/0757f9a2-106a-4938-bb50-9e8ec65177d3>
2. Check for SMS template issues: Your logs show "Sending out login email with code" instead of sending an SMS. This suggests Kratos might be routing the message incorrectly. Make sure your SMS templates are properly configured: yaml courier: templates: login_code: valid: sms: body: plaintext: "<base64://WW91ciBsb2dpbiBjb2RlIGlzOiB7eyAuTG9naW5Db2RlIH19>"
3. Verify the HTTP method: You're using PUT
in your configuration, but most webhook services expect POST
. Try changing: yaml method: PUT
to yaml method: POST
4. Check for code hooks: According to [GitHub issue #346](https://github.com/ory/network/issues/346), there can be issues with webhooks not working for registration with the code
method. Make sure your hooks are properly configured for the code method. 5. Look for more detailed logs: Enable more verbose logging to see if there are any errors when Kratos tries to send the SMS: yaml log: level: trace
If you're still having issues, you might want to try a simpler webhook endpoint for testing (like RequestBin) or consider using a different SMS provider configuration to see if the issue is specific to webhook.site.