Class: GitkitLib::RpcHelper

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

Constant Summary collapse

TOKEN_ENDPOINT =
'https://accounts.google.com/o/oauth2/token'
GITKIT_SCOPE =
'https://www.googleapis.com/auth/identitytoolkit'
GITKIT_API_URL =
'https://www.googleapis.com/identitytoolkit/v3/relyingparty/'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(service_account_email, service_account_key, server_api_key, google_token_endpoint = TOKEN_ENDPOINT) ⇒ RpcHelper

Returns a new instance of RpcHelper.



30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/rpc_helper.rb', line 30

def initialize(, , server_api_key,
    google_token_endpoint = TOKEN_ENDPOINT)
  @service_account_email = 
  @google_api_url = google_token_endpoint
  @connection = Faraday::Connection.new
  @service_account_key =
      OpenSSL::PKCS12.new(, 'notasecret').key
  @server_api_key = server_api_key
  @token_duration = 3600
  @token_issued_at = 0
  @access_token = nil
end

Instance Attribute Details

#access_tokenObject

Returns the value of attribute access_token.



23
24
25
# File 'lib/rpc_helper.rb', line 23

def access_token
  @access_token
end

#token_durationObject

Returns the value of attribute token_duration.



23
24
25
# File 'lib/rpc_helper.rb', line 23

def token_duration
  @token_duration
end

#token_issued_atObject

Returns the value of attribute token_issued_at.



23
24
25
# File 'lib/rpc_helper.rb', line 23

def token_issued_at
  @token_issued_at
end

Instance Method Details

#check_gitkit_error(response) ⇒ JSON

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Checks the Gitkit response

Parameters:

  • response (JSON)

    the response received

Returns:

  • (JSON)

    the response if no error



194
195
196
197
198
199
200
201
202
203
204
205
206
207
# File 'lib/rpc_helper.rb', line 194

def check_gitkit_error(response)
  if response.has_key? 'error'
    error = response['error']
    if error.has_key? 'code'
      code = error['code']
      raise GitkitClientError, error['message'] if code.to_s.match(/^4/)
      raise GitkitServerError, error['message']
    else
      raise GitkitServerError, 'null error code from Gitkit server'
    end
  else
    response
  end
end

#delete_account(local_id) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Delete an account

Parameters:

  • local_id (String)

    user id to be deleted



93
94
95
# File 'lib/rpc_helper.rb', line 93

def (local_id)
  invoke_gitkit_api('deleteAccount', {'localId' => local_id})
end

#download_account(next_page_token, max_results) ⇒ Array<JSON>

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Download all accounts

Parameters:

  • next_page_token (String)

    pagination token for next page

  • max_results (Fixnum)

    pagination size

Returns:

  • (Array<JSON>)

    user account info



77
78
79
80
81
82
83
84
85
86
87
# File 'lib/rpc_helper.rb', line 77

def (next_page_token, max_results)
  param = {}
  if next_page_token
    param['nextPageToken'] = next_page_token
  end
  if max_results
    param['maxResults'] = max_results
  end
  response = invoke_gitkit_api('downloadAccount', param)
  return response.fetch('nextPageToken', nil), response.fetch('users', {})
end

#fetch_access_tokenObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Get an access token, from Google server if cached one is expired



131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/rpc_helper.rb', line 131

def fetch_access_token
  if is_token_expired
    assertion = sign_assertion
    post_body = {
        'assertion' => assertion,
        'grant_type' => 'urn:ietf:params:oauth:grant-type:jwt-bearer'}
    headers = {'Content-type' => 'application/x-www-form-urlencoded'}
    response = @connection.post(RpcHelper::TOKEN_ENDPOINT, post_body,
        headers)
    @access_token = JSON.parse(response.env[:body])['access_token']
    @token_issued_at = Time.new.to_i
  end
  @access_token
end

#get_gitkit_certsJSON

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Download the Gitkit public certs

Returns:

  • (JSON)

    the public certs



178
179
180
181
182
183
184
185
186
187
# File 'lib/rpc_helper.rb', line 178

def get_gitkit_certs
  if @server_api_key.nil?
    @connection.authorization :Bearer, fetch_access_token
    response = @connection.get(GITKIT_API_URL + 'publicKeys')
  else
    response = @connection.get [GITKIT_API_URL, 'publicKeys?key=',
        @server_api_key].join
  end
  MultiJson.load response.body
end

#get_oob_code(request) ⇒ String

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Get out-of-band code for ResetPassword/ChangeEmail etc. operation

Parameters:

  • request (Hash<String, String>)

    the oob request

Returns:

  • (String)

    the oob code



66
67
68
69
# File 'lib/rpc_helper.rb', line 66

def get_oob_code(request)
  response = invoke_gitkit_api('getOobConfirmationCode', request)
  response.fetch('oobCode', nil)
end

#get_user_by_email(email) ⇒ JSON

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

GetAccountInfo by email

Parameters:

  • email (String)

    account email to be queried

Returns:

  • (JSON)

    account info



48
49
50
# File 'lib/rpc_helper.rb', line 48

def get_user_by_email(email)
  invoke_gitkit_api('getAccountInfo', {'email' => [email]})
end

#get_user_by_id(id) ⇒ JSON

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

GetAccountInfo by id

Parameters:

  • id (String)

    account id to be queried

Returns:

  • (JSON)

    account info



57
58
59
# File 'lib/rpc_helper.rb', line 57

def get_user_by_id(id)
  invoke_gitkit_api('getAccountInfo', {'localId' => [id]})
end

#invoke_gitkit_api(method, params, need_service_account = true) ⇒ JSON

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Invoke Gitkit API, with optional access token for service account operations

authenticated

Parameters:

  • method (String)

    Gitkit API method name

  • params (Hash<String, String>)

    api request params

  • need_service_account (bool) (defaults to: true)

    whether the request needs to be

Returns:

  • (JSON)

    the Gitkit api response



164
165
166
167
168
169
170
171
172
# File 'lib/rpc_helper.rb', line 164

def invoke_gitkit_api(method, params, =true)
  post_body = JSON.generate(params)
  headers = {'Content-type' => 'application/json'}
  if 
    @connection.authorization :Bearer, fetch_access_token
  end
  response = @connection.post(GITKIT_API_URL + method, post_body, headers)
  check_gitkit_error JSON.parse(response.env[:body])
end

#is_token_expiredBoolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Check whether the cached access token is expired

Returns:

  • (Boolean)

    whether the access token is expired



150
151
152
153
# File 'lib/rpc_helper.rb', line 150

def is_token_expired
  @access_token == nil ||
      Time.new.to_i > @token_issued_at + @token_duration - 30
end

#sign_assertionString

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Creates a signed jwt assertion

Returns:

  • (String)

    jwt assertion



116
117
118
119
120
121
122
123
124
125
126
# File 'lib/rpc_helper.rb', line 116

def sign_assertion
  now = Time.new
  assertion = {
      'iss' => @service_account_email,
      'scope' => GITKIT_SCOPE,
      'aud' => @google_api_url,
      'exp' => (now + @token_duration).to_i,
      'iat' => now.to_i
  }
  JWT.encode(assertion, @service_account_key, 'RS256')
end

#upload_account(hash_algorithm, hash_key, accounts) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Upload batch accounts

Parameters:

  • hash_algorithm (String)

    hash algorithm

  • hash_key (String)

    hash key

  • accounts (Array<GitkitUser>)

    account to be uploaded



103
104
105
106
107
108
109
110
# File 'lib/rpc_helper.rb', line 103

def (hash_algorithm, hash_key, accounts)
  param = {
      'hashAlgorithm' => hash_algorithm,
      'signerKey' => hash_key,
      'users' => accounts
  }
  invoke_gitkit_api('uploadAccount', param)
end