Skip to content

Instantly share code, notes, and snippets.

@PJUllrich
Last active February 25, 2021 13:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save PJUllrich/874a65bd71636aacf8e181cbaa982820 to your computer and use it in GitHub Desktop.
Save PJUllrich/874a65bd71636aacf8e181cbaa982820 to your computer and use it in GitHub Desktop.
require 'google/apis/identitytoolkit_v3'
require 'google/apis/core/api_command'
require 'googleauth'
require 'addressable/template'
require 'json'
require 'securerandom'
# Request to delete account.
class CreateSessionCookieRequest
include Google::Apis::Core::Hashable
# The GITKit token or STS id token of the authenticated user.
# Corresponds to the JSON property `idToken`
# @return [String]
attr_accessor :id_token
# A custom expiration date for the session cookie
attr_accessor :expires_in
def initialize(**args)
update!(**args)
end
# Update properties of this object
def update!(**args)
@id_token = args[:id_token] if args.key?(:id_token)
@expires_in = args[:expires_in] if args.key?(:expires_in)
end
end
class CreateSessionCookieResponse
include Google::Apis::Core::Hashable
# @return [Byte]
attr_accessor :session_cookie
def initialize(**args)
update!(**args)
end
# Update properties of this object
def update!(**args)
@session_cookie = args[:session_cookie] if args.key?(:session_cookie)
end
end
class CreateSessionCookieRequest
class Representation < Google::Apis::Core::JsonRepresentation
property :id_token, as: 'idToken'
property :valid_duration, as: 'validDuration'
end
include Google::Apis::Core::JsonObjectSupport
end
class CreateSessionCookieResponse
class Representation < Google::Apis::Core::JsonRepresentation
property :session_cookie, as: 'sessionCookie'
end
include Google::Apis::Core::JsonObjectSupport
end
class FirebaseAccountManager
SCOPE = [
'https://www.googleapis.com/auth/firebase',
'https://www.googleapis.com/auth/identitytoolkit',
'https://www.googleapis.com/auth/userinfo.email'
].join(' ')
# CREDENTIALS = ENV['GOOGLE_SERVICE_ACCOUNT_CREDS']
CREDENTIALS = 'credentials.json'
attr_accessor :service
def initialize(scope: SCOPE, credentials: CREDENTIALS)
return 'Credentials missing' if CREDENTIALS.nil?
authorizer = Google::Auth::ServiceAccountCredentials.make_creds(
json_key_io: File.open(CREDENTIALS),
scope: scope
)
@service = Google::Apis::IdentitytoolkitV3::IdentityToolkitService.new
@service.authorization = authorizer
end
def create_user(email:, password:, uid:, u_type:)
return 'Credentials missing' if CREDENTIALS.nil?
request = Google::Apis::IdentitytoolkitV3::SignupNewUserRequest.new(
email: email,
password: password,
local_id: uid,
custom_attributes: JSON.generate(
{ u_type: u_type }
)
)
@service.signup_new_user(request)
end
def get_user(uid)
request = Google::Apis::IdentitytoolkitV3::GetAccountInfoRequest.new(
local_id: [uid]
)
@service.get_account_info(request)
end
def update(email:, password:, uid:, u_type:)
request = Google::Apis::IdentitytoolkitV3::SetAccountInfoRequest.new(
email: email,
password: password,
local_id: uid,
custom_attributes: JSON.generate(
{ u_type: u_type }
)
)
@service.set_account_info(request)
end
def set_id_token(email:, password:)
request = Google::Apis::IdentitytoolkitV3::VerifyPasswordRequest.new(
email: email,
password: password
)
response = @service.verify_password(request)
@id_token = response.id_token
end
def create_or_update_account(uid, user_type, email, password)
return 'Credentials missing' if CREDENTIALS.nil?
users_response = get_user(uid)
unless users_response.users.nil?
update(
email: email,
password: password,
uid: uid,
u_type: user_type
)
end
begin
create_user(email: email, password: password, uid: uid, u_type: user_type)
rescue Google::Apis::ClientError => e
Logger.error(e.message)
end
set_id_token(email: email, password: password)
# create_session_cookie
end
def create_session_cookie
exires_in_seconds = 60 * 60 * 24 * 7
request = CreateSessionCookieRequest.new(id_token: @id_token, valid_duration: exires_in_seconds)
command = @service.send(:make_simple_command, :post, 'createSessionCookie', nil)
command.request_representation = CreateSessionCookieRequest::Representation
command.request_object = request
command.response_representation = CreateSessionCookieResponse::Representation
command.response_class = CreateSessionCookieResponse
@service.send(:execute_or_queue_command, command, &puts)
end
def make_simple_command(method, url, options)
client_version = Google::Apis::VERSION
template = Addressable::Template.new(url)
command = Google::Apis::Core::ApiCommand.new(method, template)
command.options = request_options.merge(options)
apply_command_defaults(command)
command
end
end
manager = FirebaseAccountManager.new
uid = SecureRandom.uuid
email = "peter.ullrich#{rand(1000)}@studitemps.de"
password = 'superSecret123'
url = 'https://identitytoolkit.googleapis.com/v1/projects/jobkantine:createSessionCookie'
response = manager.create_or_update_account(uid, 'student', email, password)
puts JSON.pretty_generate(response)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment