Module: WidgitAccountsSdk::Client

Extended by:
Client
Included in:
Client
Defined in:
lib/widgit_accounts_sdk/client.rb

Instance Method Summary collapse

Instance Method Details

#access_token_payload(token) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/widgit_accounts_sdk/client.rb', line 86

def access_token_payload(token)
  begin
    payload, _ = JWT.decode(
      token,
      nil,
      true,
      { algorithms: ['RS256'], jwks: method(:jwks_set) }
    )
    { valid: true, payload: payload.symbolize_keys }
  rescue JWT::ExpiredSignature
    { valid: false, error: :expired }
  rescue JWT::JWKError
    { valid: false, error: :jwk_error }
  rescue JWT::DecodeError => e
    { valid: false, error: :error }
  end
end

#create_account(params) ⇒ Object



49
50
51
52
53
# File 'lib/widgit_accounts_sdk/client.rb', line 49

def (params)
  response = request("/api/v1/accounts", :post, params)
  return failed(response['error']) if response['status'] == 'failed'
  return response
end

#exists?(email) ⇒ Boolean

Returns:

  • (Boolean)


27
28
29
30
31
# File 'lib/widgit_accounts_sdk/client.rb', line 27

def exists?(email)
  response = request("/api/v1/accounts/check?#{email.to_query(:email)}")
  return failed(response['error']) if response['status'] == 'failed'
  return success.merge('exists' => response['exists'])
end

#find_with_email(email) ⇒ Object



15
16
17
18
19
# File 'lib/widgit_accounts_sdk/client.rb', line 15

def find_with_email(email)
  response = request("/api/v1/accounts?#{email.to_query(:email)}")
  return failed(response['error']) if response['status'] == 'failed'
  return success.merge('account' => response.dig('data', 0))
end

#find_with_uid(uid) ⇒ Object



9
10
11
12
13
# File 'lib/widgit_accounts_sdk/client.rb', line 9

def find_with_uid(uid)
  response = request("/api/v1/accounts?#{uid.to_query(:uid)}")
  return failed(response['error']) if response['status'] == 'failed'
  return success.merge('account' => response.dig('data', 0))
end

#find_with_username(username) ⇒ Object



21
22
23
24
25
# File 'lib/widgit_accounts_sdk/client.rb', line 21

def find_with_username(username)
  response = request("/api/v1/accounts?#{username.to_query(:username)}")
  return failed(response['error']) if response['status'] == 'failed'
  return success.merge('account' => response.dig('data', 0))
end

#get_new_invite_token(email) ⇒ Object



43
44
45
46
47
# File 'lib/widgit_accounts_sdk/client.rb', line 43

def get_new_invite_token(email)
  response = request("/api/v1/accounts/invite?#{email.to_query(:email)}", :post)
  return response['data']['invitation_token'] if response['status'] == 'success'
  return nil
end

#invite(email, first_name = nil, last_name = nil) ⇒ Object



33
34
35
36
37
38
39
40
41
# File 'lib/widgit_accounts_sdk/client.rb', line 33

def invite(email, first_name = nil, last_name = nil)
  email = CGI.escape email # URL encode the string, for things like plus forwarding which get converted to spaces
  request_url = "/api/v1/accounts/invite?#{email.to_query(:email)}"
  request_url += "&#{first_name.to_query(:first_name)}" if first_name
  request_url += "&#{last_name.to_query(:last_name)}" if last_name
  response = request(request_url, :post)
  return failed(response['error']) if response['status'] == 'failed'
  return response
end

#refresh_access_token(refresh_token) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/widgit_accounts_sdk/client.rb', line 104

def refresh_access_token(refresh_token)
  refresh_response = request("/oauth/token", :post, {
    grant_type: 'refresh_token',
    refresh_token: refresh_token,
    redirect_uri: WidgitAccountsSdk.configuration.redirect_uri,
    client_id: WidgitAccountsSdk.configuration.client_id,
    client_secret: WidgitAccountsSdk.configuration.client_secret
  })

  if refresh_response["error"].present?
    { error: refresh_response["error"] }
  else
    {
      access_token: refresh_response["access_token"],
      refresh_token: refresh_response["refresh_token"],
      id_token: refresh_response["id_token"],
      expires_in: refresh_response["expires_in"],
      created_at: refresh_response["created_at"]
    }
  end
end

#update_account(uid, params) ⇒ Object



55
56
57
58
59
# File 'lib/widgit_accounts_sdk/client.rb', line 55

def (uid, params)
  response = request("/api/v1/accounts/#{uid}", :patch, params)
  return failed(response['error']) if response['status'] == 'failed'
  return response
end

#valid_access_token?(token) ⇒ Boolean

Returns:

  • (Boolean)


68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/widgit_accounts_sdk/client.rb', line 68

def valid_access_token?(token)
  begin
    JWT.decode(
      token,
      nil,
      true,
      { algorithms: ['RS256'], jwks: method(:jwks_set) }
    )
    true
  rescue JWT::ExpiredSignature
    :expired
  rescue JWT::JWKError
    :jwk_error
  rescue JWT::DecodeError => e
    :error
  end
end

#watch(uid, watcher_url = nil) ⇒ Object



61
62
63
64
65
66
# File 'lib/widgit_accounts_sdk/client.rb', line 61

def watch(uid, watcher_url = nil)
  watcher_url = watcher_url || WidgitAccountsSdk.configuration.watch_webhook_url
  response = request("/api/v1/accounts/#{uid}/watch?#{watcher_url.to_query(:address)}")
  return failed(response['error']) if response['status'] == 'failed'
  return response
end