Module: JsonWebToken

Defined in:
lib/json_web_token.rb,
lib/json_web_token/jwa.rb,
lib/json_web_token/jws.rb,
lib/json_web_token/jwt.rb,
lib/json_web_token/util.rb,
lib/json_web_token/version.rb,
lib/json_web_token/format/asn1.rb,
lib/json_web_token/algorithm/rsa.rb,
lib/json_web_token/algorithm/hmac.rb,
lib/json_web_token/algorithm/ecdsa.rb,
lib/json_web_token/algorithm/common.rb,
lib/json_web_token/format/base64_url.rb,
lib/json_web_token/algorithm/rsa_util.rb

Overview

Top level interface, or API, to sign and verify a JSON Web Token (JWT)

Defined Under Namespace

Modules: Algorithm, Format, Jwa, Jws, Jwt, Util

Constant Summary collapse

VERSION =
"0.3.5"

Class Method Summary collapse

Class Method Details

.sign(claims, options) ⇒ String

Returns a JSON Web Token, representing digitally signed claims.

Examples:

claims = {iss: 'joe', exp: 1300819380, :'http://example.com/is_root' => true}
options = {alg: 'HS256', key: 'gZH75aKtMN3Yj0iPS4hcgUuTwjAzZr9C'}
JsonWebToken.sign(claims, options)
# => 'eyJhbGciOiJIUzI1NiJ9.cGF5bG9hZA.uVTaOdyzp_f4mT_hfzU8LnCzdmlVC4t2itHDEYUZym4'

Parameters:

  • claims (Hash)

    a collection of name/value pairs asserting information about a subject

  • options (Hash)

    specify the desired signing algorithm and signing key

Returns:

  • (String)

    a JSON Web Token, representing digitally signed claims



17
18
19
# File 'lib/json_web_token.rb', line 17

def sign(claims, options)
  Jwt.sign(claims, options)
end

.verify(jwt, options) ⇒ Hash

Returns {ok: < the jwt claims set hash >} if the jwt verifies, or {error: ‘Invalid’} otherwise.

Examples:

jwt = 'eyJhbGciOiJIUzI1NiJ9.cGF5bG9hZA.uVTaOdyzp_f4mT_hfzU8LnCzdmlVC4t2itHDEYUZym4'
options = {alg: 'HS256', key: 'gZH75aKtMN3Yj0iPS4hcgUuTwjAzZr9C'}
JsonWebToken.verify(jwt, options)
# => {ok: {iss: 'joe', exp: 1300819380, :'http://example.com/is_root' => true}}

Parameters:

  • jwt (String)

    a JSON Web Token

  • options (Hash)

    specify the desired verifying algorithm and verifying key

Returns:

  • (Hash)

    {ok: < the jwt claims set hash >} if the jwt verifies, or {error: ‘Invalid’} otherwise



30
31
32
# File 'lib/json_web_token.rb', line 30

def verify(jwt, options)
  Jwt.verify(jwt, options)
end