Download the PHP package devmatheusguerra/jwt without Composer
On this page you can find all versions of the php package devmatheusguerra/jwt. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download devmatheusguerra/jwt
More information about devmatheusguerra/jwt
Files in devmatheusguerra/jwt
Package jwt
Short Description Uma biblioteca PHP para facilitar a utilização de autenticação JWT.
License MIT
Informations about the package jwt
Pacote PHP que implementa JSON Web Token.
Uso simples
1. Configurações Iniciais
Será necessária a criação de duas constantes globais. Você poderá defini-las num arquivo config.php, por exemplo. | CONSTANTE | TIPO | Valores Aceitos |
---|---|---|---|
SECRET_KEY_JWT | string | Qualquer valor. | |
ALGORITHM_JWT | string | HS256, HS384, HS512 |
2. Método generate()
Possui um parâmetro não obrigatório que corresponde ao dados personalizados que devem ser inseridos no payload do JWT. Caso queira ver mais sobre claim names clique aqui.
Por padrão já serão declarados alguns claim names:
- iss: Receberá o endereço do host ou localhost.
- ip: Receberá o endereço IP do solicitante.
- iat: Utilizará o instante atual da chamada da função em UNIX TIMESTAMP.
- exp: por padrão o token irá expirar em 24 horas após a chamada.
OBS.: Esses poderão ser sobre escritos.
Parâmetros
Nome | Tipo | Obrigatório |
---|---|---|
data | stdClass |
Retorno
Retorna uma string correspondente ao token gerado.
3. Método verify()
Possui um parâmetro obrigatório que corresponde ao token que foi recebido através da requisição do cliente.
Parâmetros
Nome | Tipo | Default | Obrigatório |
---|---|---|---|
token | string | - | |
ip | boolean | false |
Retorno
Retorna um stdClass. Se o parâmetro estiver false será analisada se a assinatura do token é válida e se o token não expirou. Do contrário, a requisição também deverá vir da mesma origem cujo token foi gerado.
Possíveis retornos.
4. Método getClaims()
Possui um parâmetro obrigatório que corresponde ao token que foi recebido através da requisição do cliente.
Parâmetros
Nome | Tipo | Obrigatório |
---|---|---|
token | string | |
token | string |
Retorno
Retorna uma stdClass contendo o payload do token.
5. Constantes
Buscando otimizar o tempo, alguns dos Status HTTP foram trazidos para a classe. | VALOR | HTTP STATUS CODE |
---|---|---|
FORBIDDEN | 403 | |
UNAUTHORIZED | 401 | |
BAD_REQUEST | 400 | |
SUCCESS | 200 | |
CREATED | 201 |
Exemplo de uso.