Class: GitkitLib::RpcHelper
- Inherits:
-
Object
- Object
- GitkitLib::RpcHelper
- 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
-
#access_token ⇒ Object
Returns the value of attribute access_token.
-
#token_duration ⇒ Object
Returns the value of attribute token_duration.
-
#token_issued_at ⇒ Object
Returns the value of attribute token_issued_at.
Instance Method Summary collapse
-
#check_gitkit_error(response) ⇒ JSON
private
Checks the Gitkit response.
-
#delete_account(local_id) ⇒ Object
private
Delete an account.
-
#download_account(next_page_token, max_results) ⇒ Array<JSON>
private
Download all accounts.
-
#fetch_access_token ⇒ Object
private
Get an access token, from Google server if cached one is expired.
-
#get_gitkit_certs ⇒ JSON
private
Download the Gitkit public certs.
-
#get_oob_code(request) ⇒ String
private
Get out-of-band code for ResetPassword/ChangeEmail etc.
-
#get_user_by_email(email) ⇒ JSON
private
GetAccountInfo by email.
-
#get_user_by_id(id) ⇒ JSON
private
GetAccountInfo by id.
-
#initialize(service_account_email, service_account_key, server_api_key, google_token_endpoint = TOKEN_ENDPOINT) ⇒ RpcHelper
constructor
A new instance of RpcHelper.
-
#invoke_gitkit_api(method, params, need_service_account = true) ⇒ JSON
private
Invoke Gitkit API, with optional access token for service account operations.
-
#is_token_expired ⇒ Boolean
private
Check whether the cached access token is expired.
-
#sign_assertion ⇒ String
private
Creates a signed jwt assertion.
-
#upload_account(hash_algorithm, hash_key, accounts) ⇒ Object
private
Upload batch accounts.
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(service_account_email, service_account_key, server_api_key, google_token_endpoint = TOKEN_ENDPOINT) @service_account_email = service_account_email @google_api_url = google_token_endpoint @connection = Faraday::Connection.new @service_account_key = OpenSSL::PKCS12.new(service_account_key, 'notasecret').key @server_api_key = server_api_key @token_duration = 3600 @token_issued_at = 0 @access_token = nil end |
Instance Attribute Details
#access_token ⇒ Object
Returns the value of attribute access_token.
23 24 25 |
# File 'lib/rpc_helper.rb', line 23 def access_token @access_token end |
#token_duration ⇒ Object
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_at ⇒ Object
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
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
93 94 95 |
# File 'lib/rpc_helper.rb', line 93 def delete_account(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
77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/rpc_helper.rb', line 77 def download_account(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_token ⇒ 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.
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_certs ⇒ 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 the Gitkit 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. :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
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
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
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
164 165 166 167 168 169 170 171 172 |
# File 'lib/rpc_helper.rb', line 164 def invoke_gitkit_api(method, params, need_service_account=true) post_body = JSON.generate(params) headers = {'Content-type' => 'application/json'} if need_service_account @connection. :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_expired ⇒ Boolean
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
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_assertion ⇒ 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.
Creates a signed 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
103 104 105 106 107 108 109 110 |
# File 'lib/rpc_helper.rb', line 103 def upload_account(hash_algorithm, hash_key, accounts) param = { 'hashAlgorithm' => hash_algorithm, 'signerKey' => hash_key, 'users' => accounts } invoke_gitkit_api('uploadAccount', param) end |