承接 rohsyl/laravel-otc 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

rohsyl/laravel-otc

Composer 安装命令:

composer require rohsyl/laravel-otc

包简介

Laravel One Time Code Authentication

README 文档

README

Latest Version on Packagist CI Quality Score Total Downloads

Laravel One Time Code Authentication allow you to send by mail an one time code to auth your users.

Installation

You can install the package via composer:

composer require rohsyl/laravel-otc

Run the installer

php artisan otc:install

Configuration

Edit config/otc.php

return [
    'notifier_class' => \Illuminate\Support\Facades\Notification::class,
    'notification_class' => \rohsyl\LaravelOtc\Notifications\OneTimeCodeNotification::class,

    'authenticatables' => [
        'user' => [
            'model' => \App\Models\User::class,
            'identifier' => 'email',
        ]
    ]
];

notifier_class

Define what class will be called to send the notification. By default it use the Notification facade of Laravel.

'notifier_class' => \Illuminate\Support\Facades\Notification::class,

notification_class

Define what notification will be sent.

'notification_class' => \rohsyl\LaravelOtc\Notifications\OneTimeCodeNotification::class,

You can replace this class by any other notification, you will recieve a OtcToken $token as constructor parameters

public function __construct(OtcToken $token) {
    $this->token = $token;
}

You can access the code that need to be sent from the $token variable

$token->code

authenticatables

This array will define a list of entites that can be used to get authentified. It's like a simplified version of laravel guard. I might move this to guard in the futur. The main goal is to set what model and what column are used to find the model in the database.

  • user is the name of the "guard"/type
  • model is the corresponding eloquent model
  • identifier is the identifier column that will be used to find the corresponding user
'user' => [
    'model' => \App\Models\User::class,
    'identifier' => 'email',
]

Usage

Check

Check if the user is authenticated

Otc::check()

This method will return true or false.

If the user is not authentified you can return an error

if(!Otc::check()) {
    return Otc::unauthorizedResponse($user);
}

This response will return 401 http error with the following body.

{
    "request_code_url": ".../vendor/rohsyl/laravel-otc/auth/request-code",
    "request_code_body": {
        "type": "user",
        "identifier": "test@test.com"
    }
}

You must use the request_code_url as the url to request a code (ye seem obvious) and you must pass the request_code_body as the body in json format !

Request a code

Send a post request

POST /vendor/rohsyl/laravel-otc/auth/request-code

with body

{
    "type": "user",
    "identifier": "test@test.com"
}

You need to send the type and the identifier of your authenticatables entity

An email will be sent to the corresponding entity if available. The email will contain the code.

Request a token

Send a post request

POST /vendor/rohsyl/laravel-otc/auth/code

with body

{
    "type": "user",
    "identifier": "test@test.com",
    "code": <code>
}

You need to send the code that should have been retrieved from the user through a form or anything else.

You will recieve a token back

{
    "token": "9vov6FjW47v6JjH...4iPzPH0PwpwdE"
}

And you can use this token for every further request.

Authentified request

When you have the token, you can send it with you request to be authentified.

Pass it in the headers

Authorization: Bearer <token>

Or in the query string

?token=<token>

Troubleshooting

CORS

If you use fruitcake/laravel-cors to manage CORS in your app. You will get CORS error when doing call to this package endpoints.

You will need to add a new path in your config/cors.php in the paths array

    'paths' => [
        // ...
        'vendor/rohsyl/laravel-otc/*',
    ],

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security

If you discover any security related issues, please use the issue tracker.

Credits

License

The MIT License (MIT). Please see License File for more information.

rohsyl/laravel-otc 适用场景与选型建议

rohsyl/laravel-otc 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 4.38k 次下载、GitHub Stars 达 2, 最近一次更新时间为 2022 年 11 月 28 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「laravel」 「rohsyl」 「laravel-otc」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 rohsyl/laravel-otc 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 rohsyl/laravel-otc 我们能提供哪些服务?
定制开发 / 二次开发

基于 rohsyl/laravel-otc 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 4.38k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 2
  • 点击次数: 17
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 2
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2022-11-28