Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Untrusted Host error when generate token #2031

Closed
nal-ws-001 opened this issue Sep 5, 2020 · 4 comments
Closed

Untrusted Host error when generate token #2031

nal-ws-001 opened this issue Sep 5, 2020 · 4 comments

Comments

@nal-ws-001
Copy link

nal-ws-001 commented Sep 5, 2020

I got Untrusted Host error when generate token. I need to use jwt for multiple auth.
PHP - 7.2.5
Laravel - 7.0

@devnaelson
Copy link

devnaelson commented Sep 5, 2020

you do need descibre your applications versions, example PHPX, LARAVEL 7 or 8

@devnaelson
Copy link

devnaelson commented Sep 6, 2020

Create a folder into of app folder, with name called jwt and insert it

`<?php

namespace App\jwt;

use \Tymon\JWTAuth\Facades\JWTFactory;
use \Tymon\JWTAuth\Facades\JWTAuth;

/**

  • @param author:Naelson
  • @param data:2020/09/31
  • @return credentials echo JWTProvider::key($token)->decode();
  • @return token echo JWTProvider::credentials(array("key"=>"x"))->encode();
    */

class JWTProvider
{
private static $data = array();
private $resq;
private static $token;

function __construct($request)
{
    $this->resq = $request;
}
public static function credentials($chains = array())
{
    if (!empty($chains)) {
        self::$data['sub'] = env('API_ID');
        self::$data = array_merge(self::$data, $chains);
    }
    return app(JWTProvider::class);
}
public static function key($token)
{
    self::$token = $token;
    return app(JWTProvider::class);
}
public function encode()
{
    $payload = JWTFactory::claims(self::$data)->make();
    return JWTAuth::encode($payload);
}
public function decode(): string
{
    return JWTAuth::setToken(self::$token)->getPayload();
}
/**
 * @param token required client side
 * @example instance static JWTProvider::isValid("eyJ0eXAiOi...");
 */
public static function isValid($token)
{
    try {
        JWTAuth::setToken($token)->getPayload();
        return true;
    } catch (\Exception $e) {
        return false;
    }
}

}
`

api.php

Route::group(['prefix' => 'v1'], function () {
    Route::middleware(['api'])->group(function($router){
        Route::post('test', 'AuthServer\AuthController@login');
    });
});

JWTProviderServiceProvider.php

<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;
use App\jwt\JWTProvider;
use Illuminate\Http\Request;
class JWTProviderServiceProvider extends ServiceProvider
{

    /**
     * Register services.
     *
     * @return void
     */
    public function register()
    {
        
        $this->app->bind(JWTProvider::class, function () {
            return new JWTProvider(Request());
        });
    }

    /**
     * Bootstrap services.
     *
     * @return void
     */
    public function boot()
    {
        //
    }
}

app.php

'providers' => [
   App\Providers\JWTProviderServiceProvider::class,
];

USAGE

use App\jwt\JWTProvider;

    try {
        JWTAuth::setToken($token)->getPayload();
        return true;
    } catch (\Exception $e) {
        return false;
    }

JWTProvider::key("eyJ0eXAiOi...")->decode();
JWTProvider::credentials(array("key"=>"x"))->encode();

i wait that it be useful, i will use laravel 8 more passport auth native laravel

@nal-ws-001
Copy link
Author

I still got error.
It's ok in my local. I face with that issues in production.
I generated token from Domain One. I sent that token to Domain2 and check the token using the same database and same jwt secrete key.
I use that middleware in Domain Two. ('middleware' => 'jwt.auth')

Error Logs
[2020-09-06 16:16:57] production.DEBUG: Untrusted Host "domain-name".
[2020-09-06 16:16:57] production.DEBUG: Symfony\Component\HttpFoundation\Exception\SuspiciousOper
Stack trace:
#0 /var/www/vendor/symfony/http-foundation/Request.php(998): Symfony\Component\HttpFoundation\Req
#1 /var/www/vendor/symfony/http-foundation/Request.php(1028): Symfony\Component\HttpFoundation\Re
#2 /var/www/vendor/symfony/http-foundation/Request.php(1044): Symfony\Component\HttpFoundation\Re
#3 /var/www/vendor/laravel/framework/src/Illuminate/Http/Request.php(104): Symfony\Component\Http
#4 /var/www/vendor/tymon/jwt-auth/src/Claims/Factory.php(120): Illuminate\Http\Request->url()
#5 /var/www/vendor/tymon/jwt-auth/src/Claims/Factory.php(110): Tymon\JWTAuth\Claims\Factory->iss(
#6 /var/www/vendor/tymon/jwt-auth/src/Factory.php(147): Tymon\JWTAuth\Claims\Factory->make('iss')
#7 /var/www/vendor/tymon/jwt-auth/src/Factory.php(173): Tymon\JWTAuth\Factory->buildClaims()
#8 /var/www/vendor/tymon/jwt-auth/src/Factory.php(87): Tymon\JWTAuth\Factory->buildClaimsCollecti
#9 /var/www/vendor/tymon/jwt-auth/src/Manager.php(106): Tymon\JWTAuth\Factory->make()
#10 /var/www/vendor/tymon/jwt-auth/src/JWT.php(200): Tymon\JWTAuth\Manager->decode(Object(Tymon\J
#11 /var/www/vendor/tymon/jwt-auth/src/JWT.php(136): Tymon\JWTAuth\JWT->getPayload()
#12 /var/www/vendor/tymon/jwt-auth/src/JWT.php(149): Tymon\JWTAuth\JWT->checkOrFail()
#13 /var/www/vendor/tymon/jwt-auth/src/JWTGuard.php(79): Tymon\JWTAuth\JWT->check(true)
#14 /var/www/vendor/laravel/framework/src/Illuminate/Auth/AuthManager.php(54): Tymon\JWTAuth\JWTG
#15 [internal function]: Illuminate\Auth\AuthManager->Illuminate\Auth{closure}(NULL)
#16 /var/www/vendor/laravel/framework/src/Illuminate/Auth/AuthServiceProvider.php(106): call_user
#17 [internal function]: Illuminate\Auth\AuthServiceProvider->Illuminate\Auth{closure}(NULL)
#18 /var/www/vendor/laravel/framework/src/Illuminate/Http/Request.php(518): call_user_func(Object
#19 /var/www/vendor/laravel/framework/src/Illuminate/Routing/Middleware/ThrottleRequests.php(97):
#20 /var/www/vendor/laravel/framework/src/Illuminate/Routing/Middleware/ThrottleRequests.php(49):
#21 /var/www/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(167): Illuminate\Routi
#22 /var/www/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(103): Illuminate\Pipel
#23 /var/www/vendor/laravel/framework/src/Illuminate/Routing/Router.php(687): Illuminate\Pipeline
#24 /var/www/vendor/laravel/framework/src/Illuminate/Routing/Router.php(662): Illuminate\Routing\

  • laravel.log 14/56 25%

@nal-ws-001
Copy link
Author

I got it. I changed APP_URL in env.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants