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.



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/simplicity_client.rb', line 20

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

  @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
end

Instance Method Details

#exportObject

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



38
39
40
41
42
# File 'lib/simplicity_client.rb', line 38

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

#list_accountsObject

Raises:



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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/simplicity_client.rb', line 104

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



45
46
47
48
# File 'lib/simplicity_client.rb', line 45

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

#login(username, password) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
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
# File 'lib/simplicity_client.rb', line 50

def (username, password)
  client = Aws::CognitoIdentityProvider::Client.new(region: 'ap-southeast-2')

  user_pool_id = "ap-southeast-2_abAklW6ap"

  aws_srp = Aws::CognitoSrp.new(
    username: username.downcase,
    password: password,
    pool_id: user_pool_id,
    client_id: "kvoiu7unft0c8hqqsa6hkmeu5",
    aws_client: client,
  )

  resp = aws_srp.authenticate

  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.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: username,
    access_key_id: access_key_id,
    secret_key: secret_key,
    region: "ap-southeast-2",
    session_token: session_token,
  }.transform_keys(&:to_s)
end

#logoutObject



100
101
102
# File 'lib/simplicity_client.rb', line 100

def logout
  # Not required
end