/**
  * @param array $claims
  * @return string
  */
 public function getToken(array $claims = [])
 {
     $issuer = (string) $this->request->getUri();
     $issued_at = $this->config->getTimestamp();
     $expiration = $issued_at + $this->config->getTtl();
     $key = $this->config->getPrivateKey();
     foreach ($claims as $name => $value) {
         $this->builder->set($name, $value);
     }
     $token = $this->builder->setIssuer($issuer)->setIssuedAt($issued_at)->setExpiration($expiration)->sign($this->signer, $key)->getToken();
     return (string) $token;
 }
 /**
  *
  * @param string $secret            
  * @param int $accountId            
  * @param int $arkonUserId            
  * @param \DateTimeZone $timeZone            
  */
 public function __construct($secret, $accountId, $arkonUserId, \DateTimeZone $timeZone = null)
 {
     $signer = new Sha();
     $this->token = (new Builder())->set(self::CLAIM_ACCOUNT_ID, $accountId);
     if ($arkonUserId) {
         $this->token->set(self::CLAIM_ARKON_USER_ID, $arkonUserId);
     }
     if (!is_null($timeZone)) {
         $this->token->set(self::CLAIM_TIME_ZONE, $timeZone->getName());
     }
     $date = new \DateTime(null, new \DateTimeZone(self::JWT_TIME_ZONE));
     $this->token->set(self::CLAIM_ISSUED_AT_TIME, $date->format(self::JWT_DATE_FORMAT));
     $this->token->sign($signer, $secret);
 }
Exemplo n.º 3
0
 public function createToken(AuthenticatableContract $user)
 {
     $payload = $this->buildTokenPayload($user);
     $this->builder->unsign();
     // set additional payload data
     foreach ($payload as $key => $value) {
         $this->builder->set($key, $value);
     }
     $now = time();
     $lifespanSecs = $this->config['lifespan'] * 60;
     return $this->builder->setIssuedAt($now)->setExpiration($now + $lifespanSecs)->sign(new $this->signer(), $this->secret)->getToken();
     // Retrieves the generated token
 }
Exemplo n.º 4
0
    $user = $users->findOne(['name' => $name]);
    $password = $request->get('password');
    if (null === $user || $password !== $user['password']) {
        throw new HttpException(Response::HTTP_FORBIDDEN, 'Invalid username or password.');
    }

    // Generate new JSON Web Token.
    $builder = new JWT\Builder();
    $builder
        ->setNotBefore(time())
        ->setIssuer($request->getSchemeAndHttpHost())
        ->setId($user['_id']->{'$id'})
    ;

    foreach (['name', 'email', 'given_name', 'family_name', 'email_verified', 'gender'] as $field) {
        $builder->set($field, $user[$field]);
    }

    $builder->sign(new JWT\Signer\Rsa\Sha256(), $privateKey);

    $token = $builder->getToken();
    return new Response($token, 200, ['Access-Control-Allow-Origin' => '*', 'Content-Type' => 'application/jwt']);
});

$app->get('/images', function(Request $request) use($app, $images) {
    $image = $images->find()->sort(['date' => -1]);

    $images = [];
    while ($next = $image->getNext()) {
        $images[] = ImageData::fromDb($request, $next);
    }
Exemplo n.º 5
0
 /**
  * Create object of a token.
  *
  * @param array $user
  *
  * @return Lcobucci\JWT\Token
  */
 public function generateTokenByUser($user)
 {
     return $this->builder->set('user', $user)->sign(new Sha256(), env('JWT_SECRET'))->getToken();
 }
Exemplo n.º 6
0
 /**
  * Apply claims to builder.
  * 
  * @param  array  $claims
  * @param  bool  $protect
  * @param  \Lcobucci\JWT\Builder|null  $builder
  * @return \Lcobucci\JWT\Builder
  */
 protected function applyClaims(array $claims, $protect = false, Builder $builder = null)
 {
     if (is_null($builder)) {
         $builder = new Builder();
     }
     foreach ($claims as $key => $value) {
         if ($value instanceof Claim) {
             $key = $value->getName();
             $value = $value->getValue();
         }
         if (array_key_exists($key, $this->claims)) {
             if (!$protect) {
                 $builder->{'set' . $this->claims[$key]}($value);
             }
         } else {
             $builder->set($key, $value);
         }
     }
     return $builder;
 }
Exemplo n.º 7
-1
 /**
  * @return string JWT string
  */
 public function getTokenString() : string
 {
     /**
      * Build Token
      */
     $builder = new Builder();
     $builder->setIssuer(self::ISSUER);
     $builder->setExpiration((new \DateTime($this->configuration->get('Cookie.TTL')))->getTimestamp());
     foreach ($this->values as $key => $value) {
         $builder->set($key, $value);
     }
     /**
      * Sign and generate new token
      */
     $builder->sign(new Sha256(), $this->configuration->get('Cookie.Signer.Key'));
     $token = $builder->getToken();
     return (string) $token;
 }
 /**
  * Generates a user token
  * @return string
  */
 public function getToken($data = null)
 {
     $time = time();
     $signer = new Sha256();
     $token = new Builder();
     $token->setIssuer(Router::url('/', true));
     $token->setAudience($this->request->clientIp());
     $token->setIssuedAt($time);
     $token->setNotBefore($time);
     $token->setExpiration($time + Configure::read('Security.timeout'));
     $token->set('data', json_encode($data));
     return (string) $token->sign($signer, Configure::read('Security.salt'))->getToken();
 }