Class: Passage::Client

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app_id:, api_key: "", auth_strategy: COOKIE_STRATEGY) ⇒ Client

Returns a new instance of Client.



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/passageidentity/client.rb', line 74

def initialize(app_id:, api_key: "", auth_strategy: COOKIE_STRATEGY)
  @api_url = "https://api.passage.id"
  @app_id = app_id
  @api_key = api_key

  # check for valid auth strategy
  unless [COOKIE_STRATEGY, HEADER_STRATEGY].include? auth_strategy
    raise PassageError.new(message: "invalid auth strategy.")
  end
  @auth_strategy = auth_strategy

  # setup
  get_connection

  # initialize auth class
  @auth = Passage::Auth.new(@app_id, @auth_strategy, @connection)

  # initialize user class
  @user = Passage::UserAPI.new(@connection, @app_id, @api_key)
end

Instance Attribute Details

#authObject (readonly)

Returns the value of attribute auth.



71
72
73
# File 'lib/passageidentity/client.rb', line 71

def auth
  @auth
end

#userObject (readonly)

Returns the value of attribute user.



72
73
74
# File 'lib/passageidentity/client.rb', line 72

def user
  @user
end

Instance Method Details



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/passageidentity/client.rb', line 140

def create_magic_link(
  user_id: "",
  email: "",
  phone: "",
  channel: "",
  send: false,
  magic_link_path: "",
  redirect_url: "",
  language: "",
  ttl: 60
)
  magic_link_req = {}
  magic_link_req["user_id"] = user_id unless user_id.empty?
  magic_link_req["email"] = email unless email.empty?
  magic_link_req["phone"] = phone unless phone.empty?

  # check to see if the channel specified is valid before sending it off to the server
  unless [PHONE_CHANNEL, EMAIL_CHANNEL].include? channel
    raise PassageError.new(
            message:
              "channel: must be either Passage::EMAIL_CHANNEL or Passage::PHONE_CHANNEL"
          )
  end
  magic_link_req["channel"] = channel unless channel.empty?
  magic_link_req["send"] = send
  magic_link_req[
    "magic_link_path"
  ] = magic_link_path unless magic_link_path.empty?
  magic_link_req["redirect_url"] = redirect_url unless redirect_url.empty?
  magic_link_req["ttl"] = ttl unless ttl == 0

  begin
    response =
      @connection.post("/v1/apps/#{@app_id}/magic-links", magic_link_req)
    magic_link = response.body["magic_link"]
    return(
      Passage::MagicLink.new(
        id: magic_link["id"],
        secret: magic_link["secret"],
        activated: magic_link["activated"],
        user_id: magic_link["user_id"],
        app_id: magic_link["app_id"],
        identifier: magic_link["identifier"],
        type: magic_link["type"],
        redirect_url: magic_link["redirect_url"],
        ttl: magic_link["ttl"],
        url: magic_link["url"]
      )
    )
  rescue Faraday::Error => e
    raise PassageError.new(
            message: "failed to create Passage Magic Link",
            status_code: e.response[:status],
            body: e.response[:body]
          )
  end
end

#get_appObject



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/passageidentity/client.rb', line 111

def get_app()
  begin
    app_info = @auth.fetch_app()
    return(
      Passage::App.new(
        name: app_info["name"],
        id: app_info["id"],
        auth_origin: app_info["auth_origin"],
        redirect_url: app_info["redirect_url"],
        login_url: app_info["login_url"],
        rsa_public_key: app_info["rsa_public_key"],
        allowed_identifer: app_info["allowed_identifer"],
        require_identifier_verification:
          app_info["require_identifier_verification"],
        session_timeout_length: app_info["session_timeout_length"],
        refresh_enabled: app_info["refresh_enabled"],
        refresh_absolute_lifetime: app_info["refresh_absolute_lifetime"],
        refresh_inactivity_lifetime:
          app_info["refresh_inactivity_lifetime"],
        user_metadata_schema: app_info["user_metadata_schema"],
        layouts: app_info["layouts"],
        default_language: app_info["default_language"]
      )
    )
  rescue => e
    raise e
  end
end

#get_connectionObject



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/passageidentity/client.rb', line 95

def get_connection
  gemspec = File.join(__dir__, "../../passageidentity.gemspec")
  spec = Gem::Specification.load(gemspec)
  headers = { "Passage-Version" => "passage-ruby #{spec.version}" }
  headers["Authorization"] = "Bearer #{@api_key}" if @api_key != ""

  @connection =
    Faraday.new(url: @api_url, headers: headers) do |f|
      f.request :json
      f.request :retry
      f.response :raise_error
      f.response :json
      f.adapter :net_http
    end
end