Sha256: 670515216e20d56ff0458649aa51ce242e82eca4b7cfe76efce4fc73f159b207

Contents?: true

Size: 1.24 KB

Versions: 2

Compression:

Stored size: 1.24 KB

Contents

require 'google-id-token'
require 'active_support/core_ext/module/delegation'

module GoogleSignIn
  class Identity
    class ValidationError < StandardError; end

    class_attribute :validator, default: GoogleIDToken::Validator.new

    def initialize(token)
      ensure_client_id_present
      set_extracted_payload(token)
    end

    def user_id
      @payload["sub"]
    end

    def name
      @payload["name"]
    end

    def email_address
      @payload["email"]
    end

    def email_verified?
      @payload["email_verified"] == true
    end

    def avatar_url
      @payload["picture"]
    end

    def locale
      @payload["locale"]
    end

    def hosted_domain
      @payload["hd"]
    end

    def given_name
      @payload["given_name"]
    end

    def family_name
      @payload["family_name"]
    end

    private
      delegate :client_id, to: GoogleSignIn

      def ensure_client_id_present
        if client_id.blank?
          raise ArgumentError, "GoogleSignIn.client_id must be set to validate identity"
        end
      end

      def set_extracted_payload(token)
        @payload = validator.check(token, client_id)
      rescue GoogleIDToken::ValidationError => error
        raise ValidationError, error.message
      end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
google_sign_in-1.2.1 lib/google_sign_in/identity.rb
google_sign_in-1.2.0 lib/google_sign_in/identity.rb