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



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/widgit_accounts_sdk/client.rb', line 91

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



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

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

#delete_account(uid) ⇒ Object



67
68
69
70
71
# File 'lib/widgit_accounts_sdk/client.rb', line 67

def (uid)
  response = request("/api/v1/accounts/#{uid}", :delete)
  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



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

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, **options) ⇒ Object



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

def invite(email, **options)
  params = { email: email }.merge(options.compact)
  query  = params.to_query
  request_url = "/api/v1/accounts/invite?#{query}"
  response = request(request_url, :post)
  return failed(response['error']) if response['status'] == 'failed'
  return response
end

#refresh_access_token(refresh_token) ⇒ Object



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/widgit_accounts_sdk/client.rb', line 109

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



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

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)


73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/widgit_accounts_sdk/client.rb', line 73

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



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

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