当前位置: 首页>>代码示例>>PHP>>正文


PHP JWT::encode方法代码示例

本文整理汇总了PHP中Firebase\JWT\JWT::encode方法的典型用法代码示例。如果您正苦于以下问题:PHP JWT::encode方法的具体用法?PHP JWT::encode怎么用?PHP JWT::encode使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Firebase\JWT\JWT的用法示例。


在下文中一共展示了JWT::encode方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。

示例1: login

 /**
  * @Route("/login")
  * @Method({"PUT"})
  */
 public function login(Request $request)
 {
     $email = $request->request->get('email');
     $password = $request->request->get('password');
     $customer = $this->getCustomerManager()->getRepo()->findOneByEmail($email);
     if (!$customer) {
         return $this->fail();
     }
     $em = $this->getDoctrine()->getManager();
     if ($customer->verifyPassword($password)) {
         $expire = new \DateTime('now');
         $expire->modify('+20 minute');
         $token = new Token($expire);
         if ($customer->getToken()) {
             $em->remove($customer->getToken());
         }
         $customer->setToken($token);
         $em->persist($token);
         $em->persist($customer);
         $em->flush();
         $key = $this->container->getParameter('jwt_key');
         $payload = array('type' => 'customer', 'user_id' => $customer->getId(), 'token_id' => $token->getId(), 'expires' => $token->getExpires());
         return $this->respondJson(array('jwt' => JWT::encode($payload, $key)));
     }
     return $this->fail();
 }
开发者ID:brobo,项目名称:mumshoppe,代码行数:30,代码来源:CustomerController.php

示例2: auth

 /**
  * Logs in a user with the given username and password POSTed. Though true
  * REST doesn't believe in sessions, it is often desirable for an AJAX server.
  *
  * @url POST /auth
  */
 public function auth()
 {
     $username = $_POST['username'];
     $password = $_POST['password'];
     //ToDo: handle login and generate authtoken return it
     return JWT::encode('abc', 'my_key');
 }
开发者ID:gitter-badger,项目名称:PrivateRest,代码行数:13,代码来源:RestController.php

示例3: login

 protected function login($user)
 {
     $key = Config::get('custom.JWTkey');
     $token = array("sub" => $user->id, "iss" => "http://leo.app", "iat" => 1356999524, "name" => $user->name);
     $jwt = JWT::encode($token, $key);
     return $jwt;
 }
开发者ID:abhilash698,项目名称:projectX,代码行数:7,代码来源:AuthController.php

示例4: Exception

 function login_post()
 {
     $email = '';
     $password = '';
     try {
         $entityBody = file_get_contents('php://input');
         $arrayObject = (array) json_decode($entityBody);
         if (!isset($arrayObject['email']) || !isset($arrayObject['password'])) {
             throw new Exception();
         }
         $email = $arrayObject['email'];
         $password = $arrayObject['password'];
         if ($email == '') {
             throw new Exception();
         }
         if ($password == '') {
             throw new Exception();
         }
     } catch (Exception $e) {
         $this->response(array('message' => 'Invalid request data'), self::HTTP_BAD_REQUEST);
     }
     $this->load->model('Model_users');
     $user = $this->Model_users->get_by(array('email' => $email, 'password' => sha1($password)));
     if ($user == NULL) {
         $this->response(array('message' => 'User not found'), self::HTTP_BAD_REQUEST);
     }
     $unixTime = new DateTime();
     $unixTime = $unixTime->getTimestamp();
     $token = array("uid" => $user->id, "name" => $user->name, "email" => $user->email, "iat" => $unixTime);
     $jwt = JWT::encode($token, "JWT_KEY");
     $this->response(array('id' => (int) $user->id, 'name' => $user->name, 'token' => $jwt), self::HTTP_OK);
 }
开发者ID:vitalsaude,项目名称:api,代码行数:32,代码来源:Auth.php

示例5: authenticate

 public function authenticate(Application $app, Request $request)
 {
     $username = $request->get('user');
     $password = $request->get('pass');
     if ($username && $password) {
         $sql = "SELECT * FROM user WHERE username = ? AND password = ?";
         $user = $app['db']->fetchAssoc($sql, array($username, md5($password)));
         if ($user) {
             $tokenId = base64_encode(mcrypt_create_iv(32));
             $issuedAt = time();
             $notBefore = $issuedAt;
             //Adding 10 seconds
             $expire = $notBefore + 604800;
             // Adding 1 week
             $serverName = $app['serverName'];
             // Retrieve the server name from config file
             $id = 1;
             //TODO: da cambiare
             $data = ['iat' => $issuedAt, 'jti' => $tokenId, 'iss' => $serverName, 'nbf' => $notBefore, 'exp' => $expire, 'data' => ['userId' => $user['id'], 'userName' => $user['username']]];
             // Get the secret key for signing the JWT from an environment variable
             $secretKey = base64_decode($app['secret']);
             $algorithm = $app['algorithm'];
             // Sign the JWT with the secret key
             $jwt = JWT::encode($data, $secretKey, $algorithm);
             return new JsonResponse(array('token' => $jwt), 200);
         } else {
             return new JsonResponse(array('message' => 'Failed to Authenticate'), 403);
         }
     } else {
         return new JsonResponse(array('message' => 'Failed to Authenticate'), 403);
     }
 }
开发者ID:valix,项目名称:silex-rest-jwt-skeleton,代码行数:32,代码来源:AuthenticateController.php

示例6: registerAction

 public function registerAction()
 {
     $email = $this->post_data['email'];
     $password = $this->post_data['password'];
     if ($email == NULL || $password == NULL) {
         return $this->fail('Email and password not empty', Response::HTTP_BAD_REQUEST);
     }
     $user = User::where('email', '=', $email)->first();
     if ($user) {
         return $this->fail('Email already exists ', Response::HTTP_BAD_REQUEST);
     } else {
         $user = new User();
         $user->username = $this->post_data['username'];
         $user->password = md5($this->post_data['password']);
         $user->email = $this->post_data['email'];
         $user->status = 1;
         $user->save();
         if ($user->password == md5($password) && $user->status == 1) {
             $secret = $this->app['config.app.secret'];
             $token = array('user_id' => $user->id);
             $encoded_token = JWT::encode($token, $secret, 'HS256');
             return $this->json(['mess' => 'Thành công', 'user_id' => $user->id, 'token' => $encoded_token]);
         } else {
             return $this->fail('Database error', Response::HTTP_BAD_REQUEST);
         }
     }
 }
开发者ID:anhnt0212,项目名称:rest-sifoni,代码行数:27,代码来源:AuthController.php

示例7: buildToken

 protected function buildToken(array $values = array(), $secretKey = 'ilios.jwt.key.secret')
 {
     $now = new DateTime();
     $default = array('iss' => 'ilios', 'aud' => 'ilios', 'iat' => $now->format('U'), 'exp' => $now->modify('+1 year')->format('U'), 'user_id' => 42);
     $merged = array_merge($default, $values);
     return JWT::encode($merged, $secretKey);
 }
开发者ID:stopfstedt,项目名称:ilios,代码行数:7,代码来源:JsonWebTokenManagerTest.php

示例8: encode

 /**
  * @param stdClass $payload
  * @param null $expires - can be +1 day, or +1 week, etc
  *
  * @return String
  */
 public function encode(stdClass $payload, $expires = null) : string
 {
     if (!empty($expires)) {
         $payload->exp = is_numeric($expires) ? time() + $expires : strtotime($expires);
     }
     return JWT::encode($payload, $this->key, 'HS256');
 }
开发者ID:minutephp,项目名称:framework,代码行数:13,代码来源:JwtEx.php

示例9: generateAuthHeader

 /**
  * Generate JWT authorization header
  * @return string
  */
 public function generateAuthHeader()
 {
     $time = time();
     $iss = isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : 'cli';
     $signatureData = ['sub' => $this->config['public_key'], 'iat' => $time, 'exp' => $time + $this->config['leeway'], 'iss' => $iss, 'nbf' => $time - $this->config['leeway'], 'jti' => md5($this->config['public_key'] . $time)];
     return JWT::encode($signatureData, $this->config['private_key']);
 }
开发者ID:voov,项目名称:Billingo-API-Connector,代码行数:11,代码来源:Request.php

示例10: get_profile_hmac

 private function get_profile_hmac($account)
 {
     $key = $this->apiSecret;
     $payload = array("account" => $account, "apiUniqueId" => $this->apiKey, "created" => date("DATE_W3C"));
     $jwt = JWT::encode($payload, $key);
     return $jwt;
 }
开发者ID:molekilla,项目名称:auth2factor-sdk,代码行数:7,代码来源:a2f_curl.php

示例11: login

 public function login()
 {
     $response = $this->app->response();
     $response->header("Content-Type", "application/json");
     $username = $this->app->request()->params('username');
     $password = $this->app->request()->params('password');
     if (!isset($username)) {
         return Auth::deny_access("Username is null");
     }
     if (!isset($password)) {
         return Auth::deny_access("Password is null");
     }
     $username = htmlentities(trim($username));
     $password = htmlentities(trim($password));
     $database_user = User::where('username', $username);
     $database_user = json_decode($database_user, true);
     if (empty($database_user)) {
         return ['status' => 400, 'message' => 'username doesn\'t'];
     }
     $database_user = $database_user[0];
     if ($database_user['password'] == md5($password)) {
         $key = $this->config->jwt_key();
         $token = ["iss" => $this->config->jwt_issuer(), "iat" => $this->config->jwt_issuer_at(), "nbf" => $this->config->jwt_not_before(), "exp" => $this->config->jwt_expiration_time(), "data" => ["username" => $database_user['username']]];
         $encode_jwt = JWT::encode($token, $key, 'HS512');
         $responseArray = ["token" => $encode_jwt, "status" => 200];
         $response->status(200);
         $response->body(json_encode($responseArray));
         return $response;
     } else {
         return Auth::deny_access("Incorrect Authentication Details");
     }
 }
开发者ID:emeka-osuagwu,项目名称:sweetemoji,代码行数:32,代码来源:AuthController.php

示例12: generateJwt

 protected function generateJwt($data = [], $options = [])
 {
     $options = array_merge(['alg' => 'RS256', 'key' => '4cee9dc4d2aaf2eb997113d6b76dc6fe'], $options);
     $now = time();
     $data = array_merge(['iss' => 'http://sso.7pass.dev', 'aud' => ['543d077b656332528d000000', $this->configDefault['client_id'], '564f035a0640fd25df00000c', '565454f10640fd1cb500043a', '56b4824f0640fd4d460000b2'], 'exp' => $now + 10, 'iat' => $now, 'auth_time' => $now, 'nonce' => 'z8jw9aVPJ9vmz01d', 'azp' => $this->configDefault['client_id'], 'sub' => '56d6c9a07eddd9781d507ea5', 'gender' => 'male', 'birthdate' => '1964-07-03', 'first_name' => 'Test', 'last_name' => 'User', 'birthdate_verified' => false, 'email' => 'test@example.com', 'email_verified' => false, 'at_hash' => 'k4taUhrRarXC78p5qFyUfg'], $data);
     return JWT::encode($data, file_get_contents(__DIR__ . '/fixtures/certs/jwk.pem'), $options['alg'], $options['key']);
 }
开发者ID:p7s1-ctf,项目名称:7pass-php-sdk,代码行数:7,代码来源:bootstrap.php

示例13: createToken

 protected function createToken($request)
 {
     $secret_key = getenv('SECRET_KEY');
     $token = array('iss' => getenv('AUTH_ISS'), 'aud' => $request->getUri()->getHost(), 'iat' => time(), 'nbf' => time(), 'exp' => time() + 3600);
     $jwt = JWT::encode($token, $secret_key);
     return $jwt;
 }
开发者ID:aodkrisda,项目名称:slim3-api-skeleton,代码行数:7,代码来源:Auth.php

示例14: authorizationEncode

 /**
  * authorizationEncode Generate token using $userID
  *
  * @param  $userID
  *
  * @return string
  */
 public function authorizationEncode($userID)
 {
     if (!is_null($userID)) {
         $token = array("iss" => $this->getIssuedBy(), "aud" => $this->getAuthUrl(), "user" => $userID, "exp" => time() + 3600000);
     }
     return JWT::encode($token, $this->getKey());
 }
开发者ID:andela-iadeniyi,项目名称:emojinaija,代码行数:14,代码来源:AuthController.php

示例15: Login

 public function Login($user, $pass)
 {
     $output = array();
     $this->db->join('user', 'superuser.ID_SU = user.ID_USU', 'left');
     $this->db->where('SUname', $user);
     $this->db->where('pswd', md5($pass));
     $query = $this->db->get('superuser');
     if ($query->num_rows() == 0) {
         return false;
     }
     $output = $query->result();
     $tokenId = base64_encode(mcrypt_create_iv(32));
     $issuedAt = time();
     $notBefore = $issuedAt;
     // Is valid right away
     $expire = $notBefore + 60 * 60 * 24 * 1;
     // Token expires in 1 week
     $serverName = 'myserver';
     // Retrieve the server name from config file
     /*
      * Create the token as an array
      */
     $data = ['iat' => $issuedAt, 'jti' => $tokenId, 'iss' => $serverName, 'nbf' => $notBefore, 'exp' => $expire, 'data' => ['userId' => $output[0]->ID_SU, 'userName' => $output[0]->SUname]];
     $secretKey = base64_decode('lamevaclausupersecreta');
     $jwt = JWT::encode($data, $secretKey, 'HS512');
     $unencodedArray = ['token' => $jwt];
     return $unencodedArray;
 }
开发者ID:narum,项目名称:jocomunicoLAB,代码行数:28,代码来源:user_model.php


注:本文中的Firebase\JWT\JWT::encode方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。