【问题标题】:Authentication in laravellaravel 中的身份验证
【发布时间】:2016-09-01 04:12:15
【问题描述】:

我正在 laravel 中制作 api。我正在使用已经构建并处于活动状态的数据库。该系统使用带有盐值的 md5。 我想对该系统进行身份验证。我该怎么办? 我的源代码:

public function authenticate(Request $request)
{
 $email = $request->input('email');
 $password = md5('testpassword' . 'saltvaluehere');
 try {
        //

        // attempt to verify the credentials and create a token for the user
        if (!$token = JWTAuth::attempt([
            'email' => $email,
            'pass' => $password
        ])
        ) {


            return response()->json(['error' => 'invalid_credentials'], 401);
        }
    } catch (JWTException $e) {
        // something went wrong whilst attempting to encode the token
        return response()->json(['error' => 'could_not_create_token'], 500);
    }

    // all good so return the token
    return response()->json(compact('token'));
}

在核心 php 中,这些身份验证通过以下方式完成:

$getpass        = $mydb->CleanString($_POST['password']); 
$getusername    = $mydb->CleanString($_POST['username']);
$dbpass = $mydb->md5_decrypt($mydb->getValue("pass","tbl_admin","username = '".$getusername."'"), SECRETPASSWORD);

    if($dbpass == $getpass){
return 'success full login';
}

上面的代码没有给出相同的哈希值,所以我无法在系统中进行身份验证。

已编辑:

我得到了与数据库匹配的密码,但没有生成令牌。

这是我的代码:

 public function authenticate(Request $request)
{

    $email = $request->input('email');
    $password = $request->input('password');
    $password = md5($password);



    try {
        //

        // attempt to verify the credentials and create a token for the user
        if (!$token = JWTAuth::attempt([
            'email' => $email,
            'password' => $password
        ])
        ){

            //return response()->json(compact('token'));
           return response()->json(['error' => 'invalid_credentials','_data'=>[$password,$email]], 401);
        }
    } catch (JWTException $e) {
        // something went wrong whilst attempting to encode the token
        return response()->json(['error' => 'could_not_create_token'], 500);
    }

    // all good so return the token
    return response()->json(compact('token'));
}

谁能告诉我为什么没有生成令牌以及为什么它说无效的凭据。因为它显示了加密形式的数据库的密码和电子邮件。

【问题讨论】:

    标签: api laravel authentication encryption login


    【解决方案1】:

    这是我处理过的情况,我有 foss 代码作为概念证明。

    基本上,我为旧密码添加了另一个密码字段。我导入了 password_legacy 设置为旧密码数据的 JSON 对象的用户,在我的例子中:
    { 'hasher' : 'algo', 'hash' : '#####', 'salt' : 'salt here' }

    然后,我使用了修改后的用户服务提供程序,以便在 password 为空时检查 password_legacy 的身份验证。然后它使用 Illuminate Hasher 合约作为基础并构造一个类的新实例(动态地,使用hasher 属性,例如app\Services\Hashing\AlgoHasher),然后将其用于身份验证而不是默认系统。

    如果成功,我bcrypt'd 密码,设置password 和取消设置password_legacy。这将密码升级到 bcrypt 提供的更高的安全标准。

    新字段的迁移代码:
    https://github.com/infinity-next/infinity-next/blob/ccb01c753bd5cedd75e03ffe562f389d690de585/database/migrations_0_3_0/2015_06_12_181054_password_legacy.php

    修改用户提供者:
    https://github.com/infinity-next/infinity-next/blob/ccb01c753bd5cedd75e03ffe562f389d690de585/app/Providers/EloquentUserProvider.php

    Vichan 的哈希合约(md5+salt):
    https://github.com/infinity-next/infinity-next/blob/ccb01c753bd5cedd75e03ffe562f389d690de585/app/Services/Hashing/VichanHasher.php

    相关用户代码:
    https://github.com/infinity-next/infinity-next/blob/ccb01c753bd5cedd75e03ffe562f389d690de585/app/User.php#L124-L165

    希望有帮助!

    【讨论】:

      猜你喜欢
      • 2016-05-16
      • 2016-08-03
      • 2019-08-12
      • 2016-08-05
      • 2019-09-06
      • 1970-01-01
      • 2019-10-10
      相关资源
      最近更新 更多