Class: GitkitLib::GitkitClient

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client_id, service_account_email, service_account_key, widget_url, server_api_key = nil) ⇒ GitkitClient

Initializes a GitkitClient.

Parameters:

  • client_id (String)

    Google oauth2 web client id of this site

  • service_account_email (String)

    Google service account email

  • service_account_key (String)

    Google service account private p12 key

  • widget_url (String)

    url to host the Gitkit widget

  • server_api_key (String) (defaults to: nil)

    server-side Google API key



47
48
49
50
51
52
53
54
# File 'lib/gitkit_client.rb', line 47

def initialize(client_id, , ,
    widget_url, server_api_key = nil)
  @client_id = client_id
  @widget_url = widget_url
  @rpc_helper = RpcHelper.new(, ,
      server_api_key)
  @certificates = {}
end

Class Method Details

.create_from_config_file(file) ⇒ Object

Create a client from json config file

Parameters:

  • file (String)

    file name of the json-format config



28
29
30
31
32
33
34
35
36
37
38
# File 'lib/gitkit_client.rb', line 28

def self.create_from_config_file(file)
  config = JSON.parse File.open(file, 'rb') { |io| io.read }
  p12key = File.open(config['serviceAccountPrivateKeyFile'], 'rb') {
      |io| io.read }
  new(
      config['clientId'],
      config['serviceAccountEmail'],
      p12key,
      config['widgetUrl'],
      config['serverApiKey'])
end

Instance Method Details



206
207
208
209
210
211
212
213
214
215
# File 'lib/gitkit_client.rb', line 206

def build_oob_link(full_url, param, mode)
  code = @rpc_helper.get_oob_code(param)
  if code
    oob_link = Addressable::URI.parse full_url
    oob_link.path = @widget_url
    oob_link.query_values = { 'mode' => mode, 'oobCode' => code}
    return oob_link.to_s
  end
  nil
end

#change_email_request(param, user_ip, gitkit_token) ⇒ Object



196
197
198
199
200
201
202
203
204
# File 'lib/gitkit_client.rb', line 196

def change_email_request(param, user_ip, gitkit_token)
  {
    'email' => param['oldEmail'],
    'newEmail' => param['newEmail'],
    'userIp' => user_ip,
    'idToken' => gitkit_token,
    'requestType' => 'NEW_EMAIL_ACCEPT'
  }
end

#delete_user(local_id) ⇒ Object

Deletes a user account from Gitkit service

Parameters:

  • local_id (String)

    user id to be deleted



145
146
147
# File 'lib/gitkit_client.rb', line 145

def delete_user(local_id)
  @rpc_helper. local_id
end

#email_change_response(oob_link, param) ⇒ Object



221
222
223
224
225
226
227
228
229
# File 'lib/gitkit_client.rb', line 221

def email_change_response(oob_link, param)
  {
    :oldEmail => param['email'],
    :newEmail => param['newEmail'],
    :oobLink => oob_link,
    :action => :CHANGE_EMAIL,
    :response_body => {'success' => true}.to_json
  }
end

#failure_msg(msg) ⇒ Object



217
218
219
# File 'lib/gitkit_client.rb', line 217

def failure_msg(msg)
  {:response_body => {'error' => msg}}.to_json
end

#get_all_users(max_results = 10) {|GitkitUser| ... } ⇒ Object

Downloads all user accounts from Gitkit service

Parameters:

  • max_results (Fixnum) (defaults to: 10)

    pagination size of each request

Yields:



118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/gitkit_client.rb', line 118

def get_all_users(max_results = 10)
  next_page_token = nil
  while true
    next_page_token, accounts = @rpc_helper.(
        next_page_token, max_results)
    accounts.each { ||
      yield GitkitUser.parse_from_api_response  }
    if not next_page_token or accounts.length == 0
      break
    end
  end
end

#get_certsObject

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.

Gets Gitkit public certs



92
93
94
# File 'lib/gitkit_client.rb', line 92

def get_certs
  @rpc_helper.get_gitkit_certs()
end

#get_oob_result(full_url, param, user_ip, gitkit_token = nil) ⇒ Hash

Get one-time out-of-band code for ResetPassword/ChangeEmail request

}

Parameters:

  • full_url (String)

    the full URL of incoming request

  • param (Hash{String=>String})

    dict of HTTP POST params

  • user_ip (String)

    end user’s IP address

  • gitkit_token (String) (defaults to: nil)

    the gitkit token if user logged in

Returns:

  • (Hash)

    { email: user email who is asking reset password oobLink: the generated link to be send to user’s email action: OobAction response_body: the http body to be returned



162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/gitkit_client.rb', line 162

def get_oob_result(full_url, param, user_ip, gitkit_token=nil)
  if param.has_key? 'action'
    begin
      if param['action'] == 'resetPassword'
        oob_link = build_oob_link(full_url,
            password_reset_request(param, user_ip),
            param['action'])
        return password_reset_response(oob_link, param)
      elsif param['action'] == 'changeEmail'
        unless gitkit_token
          return failure_msg('login is required')
        end
        oob_link = build_oob_link(full_url,
            change_email_request(param, user_ip, gitkit_token),
            param['action'])
        return email_change_response(oob_link, param)
      end
    rescue GitkitClientError => error
      return failure_msg(error.message)
    end
  end
  failure_msg('unknown request type')
end

#get_user_by_email(email) ⇒ GitkitUser

Gets user info by email

Parameters:

  • email (String)

    user email

Returns:



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

def get_user_by_email(email)
  response = @rpc_helper.get_user_by_email email
  GitkitUser.parse_from_api_response response.fetch('users', [{}])[0]
end

#get_user_by_id(id) ⇒ GitkitUser

Gets user info by user id

Parameters:

  • id (String)

    user id

Returns:



109
110
111
112
# File 'lib/gitkit_client.rb', line 109

def get_user_by_id(id)
  response = @rpc_helper.get_user_by_id id
  GitkitUser.parse_from_api_response response.fetch('users', [{}])[0]
end

#password_reset_request(param, user_ip) ⇒ Object



186
187
188
189
190
191
192
193
194
# File 'lib/gitkit_client.rb', line 186

def password_reset_request(param, user_ip)
  {
    'email' => param['email'],
    'userIp' => user_ip,
    'challenge' => param['challenge'],
    'captchaResp' => param['response'],
    'requestType' => 'PASSWORD_RESET'
  }
end

#password_reset_response(oob_link, param) ⇒ Object



231
232
233
234
235
236
237
238
# File 'lib/gitkit_client.rb', line 231

def password_reset_response(oob_link, param)
  {
    :email => param['email'],
    :oobLink => oob_link,
    :action => :RESET_PASSWORD,
    :response_body => {'success' => true}.to_json
  }
end

#upload_users(hash_algorithm, hash_key, accounts) ⇒ Object

Uploads multiple accounts to Gitkit service

Parameters:

  • hash_algorithm (String)

    password hash algorithm

  • hash_key (String)

    key of the hash algorithm

  • accounts (Array<GitkitUser>)

    user accounts to be uploaded



136
137
138
139
140
# File 'lib/gitkit_client.rb', line 136

def upload_users(hash_algorithm, hash_key, accounts)
   = accounts.collect { || .to_request }
  @rpc_helper. hash_algorithm, JWT.base64url_encode(hash_key),
      
end

#verify_gitkit_token(token_string) ⇒ GitkitUser?

Verifies a Gitkit token

error_message.

Parameters:

  • token_string (String)

    the token to be verified

Returns:

  • (GitkitUser, nil)

    for valid token, [nil, String] otherwise with



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
# File 'lib/gitkit_client.rb', line 61

def verify_gitkit_token(token_string)
  if token_string.nil?
    return nil, 'no token'
  end
  begin
    parsed_token, _ = JWT.decode(token_string, nil, true) {|header|
      # key finder
      key_id = header['kid']
      unless @certificates.has_key? key_id
        @certificates = Hash[get_certs.map {|key,cert|
            [key, OpenSSL::X509::Certificate.new(cert)]}]
      end
      @certificates[key_id].public_key
    }
    # check expiration time
    if Time.new.to_i > parsed_token['exp']
      return nil, 'token expired'
    end
    # check audience
    if parsed_token['aud'] != @client_id
      return nil, 'audience mismatch'
    end
    GitkitUser.parse_from_api_response parsed_token
  rescue JWT::DecodeError => e
    return nil, e.message
  end
end