Class: Himari::AccessToken

Inherits:
Object
  • Object
show all
Includes:
TokenString
Defined in:
lib/himari/access_token.rb

Defined Under Namespace

Classes: Bearer

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from TokenString

#format, included, #magic_header, #secret, #secret_hash, #verify!, #verify_expiry!, #verify_secret!

Constructor Details

#initialize(handle:, client_id:, claims:, expiry:, secret: nil, secret_hash: nil) ⇒ AccessToken

Returns a new instance of AccessToken.



35
36
37
38
39
40
41
42
43
# File 'lib/himari/access_token.rb', line 35

def initialize(handle:, client_id:, claims:, expiry:, secret: nil, secret_hash: nil)
  @handle = handle
  @client_id = client_id
  @claims = claims
  @expiry = expiry

  @secret = secret
  @secret_hash = secret_hash
end

Instance Attribute Details

#claimsObject (readonly)

Returns the value of attribute claims.



45
46
47
# File 'lib/himari/access_token.rb', line 45

def claims
  @claims
end

#client_idObject (readonly)

Returns the value of attribute client_id.



45
46
47
# File 'lib/himari/access_token.rb', line 45

def client_id
  @client_id
end

#expiryObject (readonly)

Returns the value of attribute expiry.



45
46
47
# File 'lib/himari/access_token.rb', line 45

def expiry
  @expiry
end

#handleObject (readonly)

Returns the value of attribute handle.



45
46
47
# File 'lib/himari/access_token.rb', line 45

def handle
  @handle
end

Class Method Details

.default_lifetimeObject



22
23
24
# File 'lib/himari/access_token.rb', line 22

def self.default_lifetime
  3600
end

.from_authz(authz) ⇒ Object

Parameters:



27
28
29
30
31
32
33
# File 'lib/himari/access_token.rb', line 27

def self.from_authz(authz)
  make(
    client_id: authz.client_id,
    claims: authz.claims,
    lifetime: authz.lifetime.access_token,
  )
end

.magic_headerObject



18
19
20
# File 'lib/himari/access_token.rb', line 18

def self.magic_header
  'hmat'
end

Instance Method Details

#as_jsonObject



69
70
71
72
73
74
75
76
77
# File 'lib/himari/access_token.rb', line 69

def as_json
  {
    handle: handle,
    secret_hash: secret_hash,
    client_id: client_id,
    claims: claims,
    expiry: expiry.to_i,
  }
end

#as_logObject



60
61
62
63
64
65
66
67
# File 'lib/himari/access_token.rb', line 60

def as_log
  {
    handle: handle,
    client_id: client_id,
    claims: claims,
    expiry: expiry,
  }
end

#to_bearerObject



53
54
55
56
57
58
# File 'lib/himari/access_token.rb', line 53

def to_bearer
  Bearer.new(
    access_token: format.to_s,
    expires_in: (expiry - Time.now.to_i).to_i,
  )
end

#userinfoObject



47
48
49
50
51
# File 'lib/himari/access_token.rb', line 47

def userinfo
  claims.merge(
    aud: client_id,
  )
end