|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +module IdentityPlatform |
| 4 | + #= IdentityPlatform::Token |
| 5 | + # |
| 6 | + # The tokens we obtain when authenticating users through Google Cloud Identity |
| 7 | + # Platform |
| 8 | + class Token |
| 9 | + include ActiveModel::Model |
| 10 | + include ActiveModel::Attributes |
| 11 | + include ActiveModel::Validations::Callbacks |
| 12 | + |
| 13 | + ISSUER_PREFIX = 'https://securetoken.google.com/' |
| 14 | + |
| 15 | + PAYLOAD_KEY_MAP = { |
| 16 | + 'iss' => 'issuer', |
| 17 | + 'sub' => 'subject', |
| 18 | + 'aud' => 'audience', |
| 19 | + 'iat' => 'issued_at', |
| 20 | + 'exp' => 'expires_at', |
| 21 | + 'auth_time' => 'authenticated_at' |
| 22 | + }.freeze |
| 23 | + |
| 24 | + PAYLOAD_MAPPER = proc { |key| PAYLOAD_KEY_MAP.fetch key, key } |
| 25 | + |
| 26 | + # Transient attributes: |
| 27 | + attr_accessor :token, :payload, :header |
| 28 | + |
| 29 | + attribute :issuer, type: :string |
| 30 | + attribute :subject, type: :string |
| 31 | + attribute :audience, type: :string |
| 32 | + attribute :issued_at, type: :datetime |
| 33 | + attribute :expires_at, type: :datetime |
| 34 | + attribute :authenticated_at, type: :datetime |
| 35 | + attribute :created_at, type: :datetime |
| 36 | + |
| 37 | + before_validation :extract_token_payload |
| 38 | + |
| 39 | + def self.load(given_token) |
| 40 | + new(token: given_token) |
| 41 | + end |
| 42 | + |
| 43 | + def self.decode_token_with_cert(token, key, cert) |
| 44 | + public_key = cert.public_key |
| 45 | + |
| 46 | + JWT.decode( |
| 47 | + token, |
| 48 | + public_key, |
| 49 | + !public_key.nil?, |
| 50 | + decoding_options.merge(kid: key) |
| 51 | + ) |
| 52 | + end |
| 53 | + |
| 54 | + def self.expected_audience |
| 55 | + ENV.fetch 'GOOGLE_CLOUD_PROJECT', 'fir-rails-f5432' |
| 56 | + end |
| 57 | + |
| 58 | + def self.expected_issuer |
| 59 | + "#{ISSUER_PREFIX}#{expected_audience}" |
| 60 | + end |
| 61 | + |
| 62 | + def self.decoding_options |
| 63 | + { |
| 64 | + algorithm: 'RS256', |
| 65 | + iss: expected_issuer, |
| 66 | + aud: expected_audience, |
| 67 | + verify_aud: true, |
| 68 | + verify_iss: true |
| 69 | + } |
| 70 | + end |
| 71 | + |
| 72 | + delegate :certs, to: CertStore |
| 73 | + delegate :decode_token_with_cert, to: :class |
| 74 | + |
| 75 | + private |
| 76 | + |
| 77 | + def extract_token_payload |
| 78 | + decode_token_with_certs |
| 79 | + return errors.add(:token, 'invalid token') if payload.blank? |
| 80 | + |
| 81 | + assign_attributes string_attributes_from_payload |
| 82 | + assign_attributes timestamp_attributes_from_payload |
| 83 | + end |
| 84 | + |
| 85 | + def string_attributes_from_payload |
| 86 | + payload.slice(*%w[iss sub aud]).transform_keys(&PAYLOAD_MAPPER) |
| 87 | + end |
| 88 | + |
| 89 | + def timestamp_attributes_from_payload |
| 90 | + payload |
| 91 | + .slice(*%w[iat exp auth_time]) |
| 92 | + .transform_keys(&PAYLOAD_MAPPER) |
| 93 | + .transform_values { |value| Time.at(value) } |
| 94 | + end |
| 95 | + |
| 96 | + def decode_token_with_certs |
| 97 | + certs.detect do |key, cert| |
| 98 | + assign_payload_and_header_with_key_and_cert(key, cert) |
| 99 | + break if payload.present? || errors.any? |
| 100 | + end |
| 101 | + end |
| 102 | + |
| 103 | + def assign_payload_and_header_with_key_and_cert(key, cert) |
| 104 | + return if payload.present? |
| 105 | + |
| 106 | + @payload, @header = decode_token_with_cert(token, key, cert) |
| 107 | + @payload = @payload&.with_indifferent_access |
| 108 | + rescue JWT::ExpiredSignature |
| 109 | + errors.add :token, 'signature expired' |
| 110 | + rescue JWT::InvalidIssuerError |
| 111 | + errors.add :token, 'invalid issuer' |
| 112 | + rescue JWT::DecodeError |
| 113 | + nil |
| 114 | + end |
| 115 | + end |
| 116 | +end |
0 commit comments