Class: SimplicityClient::Session

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

Instance Method Summary collapse

Constructor Details

#initializeSession

Returns a new instance of Session.



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/simplicity_client.rb', line 33

def initialize
  @logger = Logger.new $stderr
  @logger.level = Logger::DEBUG

  @user_agent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36"

  @client = Faraday.new(
    headers: { "User-Agent" => @user_agent },
    # proxy: "http://Thinkbook.local:8080",
    # :ssl => {:verify => false}
  ) do |builder|
    builder.response :follow_redirects
    builder.response :logger
    builder.adapter Faraday.default_adapter
  end

  @authsignal_base = "https://au.api.authsignal.com/v1"
  @authsignal_tenant_id = "e768822f-b1a1-404c-a685-0e37905ca5f5"
end

Instance Method Details

#exportObject

Write the session data out to a string so that the session can be restored later



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

def export
  {
    auth_data: @auth_data,
  }.to_json
end

#list_accountsObject

Raises:



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
197
198
199
# File 'lib/simplicity_client.rb', line 147

def list_accounts
  # Ensure @auth_data contains the necessary credentials
  raise Error, "Authentication data not found. Please login first." unless @auth_data

  service = "execute-api"
  region = "ap-southeast-2"
  http_method = "POST"
  url = "https://h4ku5ofov2.execute-api.ap-southeast-2.amazonaws.com/prod/secure"
  email = @auth_data["email"]
  body = {
    variables: {},
    query: "{\n  Account(email: \"#{email}\") {\n    InvestmentType\n    InvestmentCode\n    PortfolioCode\n    InvestmentName\n    Portfolio\n    Status\n    RegistryAccountId\n    MarketValue\n    PrimaryBeneficiarySurname\n    EntityType\n    PriceDate\n    IsDefault\n    ExternalReference\n    BankAccounts {\n      AccountName\n      LastThreeDigitsOfAccountNumberPart\n      SuffixPart\n      IsPrimary\n      Id\n      __typename\n    }\n    __typename\n  }\n}\n",
  }.to_json

  signer = Aws::Sigv4::Signer.new(
    service: service,
    region: region,
    access_key_id: @auth_data["access_key_id"],
    secret_access_key: @auth_data["secret_key"],
    session_token: @auth_data["session_token"],
  )

  signature = signer.sign_request(
    http_method: http_method,
    url: url,
    body: body,
  )

  response = @client.post(url) do |req|
    req.headers = signature.headers.merge({
      "Content-Type" => "application/json",
    })
    req.body = body
  end

  # Handle the response
  if response.success?
    obj = JSON.parse(response.body)
    obj["data"]["Account"].map do ||
      {
        accountNo: ["InvestmentCode"],
        accountType: "#{["InvestmentType"]} - #{["Portfolio"]}",
        customerName: ["InvestmentName"],
        accountBalance: ["MarketValue"],
        isLiabilityType: false,
        supportsTransactions: false,
        dynamicBalance: true,
      }
    end
  else
    raise Error, "Failed to list accounts: #{response.status}, #{response.body}"
  end
end

#load(str) ⇒ Object

Load the session data from a string to restore a session



61
62
63
64
# File 'lib/simplicity_client.rb', line 61

def load(str)
  json = JSON.parse(str)
  @auth_data = json["auth_data"]
end

#login(params) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
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
139
140
141
# File 'lib/simplicity_client.rb', line 66

def (params)
  client = Aws::CognitoIdentityProvider::Client.new(region: "ap-southeast-2")

  user_pool_id = "ap-southeast-2_abAklW6ap"

  device_id = SecureRandom.uuid
  passkey = Fido2Client::Passkey.new(params.credentialId, params.keyAlgorithm, params.keyCurve, params.keyValue, params.userHandle)
  challenge_id = fetch_challenge_id
  authentication_options = fetch_authentication_options(challenge_id)
  challenge = authentication_options["options"]["challenge"]
  assertion = Fido2Client::Client.new.get_assertion(passkey, challenge)
  passkey_result = present_passkey(challenge_id, assertion, device_id)

  resp = client.initiate_auth(
    {
      auth_flow: "CUSTOM_AUTH",
      auth_parameters: {
        USERNAME: params.email,
      },
      client_metadata: {
        anonymousId: device_id,
      },
      client_id: "kvoiu7unft0c8hqqsa6hkmeu5",
    },
  )

  resp = client.respond_to_auth_challenge(
    {
      challenge_name: "CUSTOM_CHALLENGE",
      challenge_responses: {
        USERNAME: params.email,
        ANSWER: passkey_result["accessToken"],
      },
      client_id: "kvoiu7unft0c8hqqsa6hkmeu5",
      session: resp.session,
    },
  )

  cognito_identity_client = Aws::CognitoIdentity::Client.new(region: "ap-southeast-2")

  # Assuming you have the Identity Pool ID and the ID token
  identity_pool_id = "ap-southeast-2:0ed33fc6-4cef-4f2e-b634-31c616e108e2"
  id_token = resp.authentication_result.id_token

  # Get ID from the identity pool
  id_response = cognito_identity_client.get_id(
    {
      identity_pool_id: identity_pool_id,
      logins: {
        "cognito-idp.ap-southeast-2.amazonaws.com/#{user_pool_id}" => id_token,
      },
    },
  )

  # Get credentials for the ID
  credentials_response = cognito_identity_client.get_credentials_for_identity(
    {
      identity_id: id_response.identity_id,
      logins: {
        "cognito-idp.ap-southeast-2.amazonaws.com/#{user_pool_id}" => id_token,
      },
    },
  )

  access_key_id = credentials_response.credentials.access_key_id
  secret_key = credentials_response.credentials.secret_key
  session_token = credentials_response.credentials.session_token

  @auth_data = {
    email: params.email,
    access_key_id: access_key_id,
    secret_key: secret_key,
    region: "ap-southeast-2",
    session_token: session_token,
  }.transform_keys(&:to_s)
end

#logoutObject



143
144
145
# File 'lib/simplicity_client.rb', line 143

def logout
  # Not required
end