Class: Passage::Auth

Inherits:
Object
  • Object
show all
Defined in:
lib/passageidentity/auth.rb

Overview

The Passage::Auth class provides methods for authenticating requests and tokens

Instance Method Summary collapse

Constructor Details

#initialize(app_id:, req_opts:) ⇒ Auth

Returns a new instance of Auth.



11
12
13
14
15
16
17
18
19
# File 'lib/passageidentity/auth.rb', line 11

def initialize(app_id:, req_opts:)
  @app_id = app_id
  @req_opts = req_opts

  @app_cache = ActiveSupport::Cache::MemoryStore.new
  fetch_jwks

  @magic_links_client = OpenapiClient::MagicLinksApi.new
end

Instance Method Details



40
41
42
43
44
45
46
47
48
# File 'lib/passageidentity/auth.rb', line 40

def create_magic_link_with_email(email:, type:, send:, opts: {})
  args = {}
  args['email'] = email
  args['channel'] = 'email'
  args['type'] = type
  args['send'] = send

  create_magic_link(args, opts)
end


50
51
52
53
54
55
56
57
58
# File 'lib/passageidentity/auth.rb', line 50

def create_magic_link_with_phone(phone:, type:, send:, opts: {})
  args = {}
  args['phone'] = phone
  args['channel'] = 'phone'
  args['type'] = type
  args['send'] = send

  create_magic_link(args, opts)
end

Raises:

  • (ArgumentError)


60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/passageidentity/auth.rb', line 60

def create_magic_link_with_user(user_id:, channel:, type:, send:, opts: {})
  raise ArgumentError, "channel must be either 'email' or 'phone'" unless %w[
    email
    phone
  ].include?(channel)

  args = {}
  args['user_id'] = user_id
  args['channel'] = channel
  args['type'] = type
  args['send'] = send

  create_magic_link(args, opts)
end

#validate_jwt(jwt:) ⇒ Object

Raises:

  • (ArgumentError)


21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/passageidentity/auth.rb', line 21

def validate_jwt(jwt:)
  raise ArgumentError, 'jwt is required.' unless jwt && !jwt.empty?

  claims =
    JWT.decode(
      jwt,
      nil,
      true,
      {
        aud: @app_id,
        verify_aud: true,
        algorithms: ['RS256'],
        jwks: fetch_jwks
      }
    )

  claims[0]['sub']
end