summaryrefslogtreecommitdiff
path: root/lib/gitlab/auth/atlassian/user.rb
blob: 6ab7741cc547b331c549e6cc6f1ab113b5f731a0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# frozen_string_literal: true

module Gitlab
  module Auth
    module Atlassian
      class User < Gitlab::Auth::OAuth::User
        def self.assign_identity_from_auth_hash!(identity, auth_hash)
          identity.extern_uid = auth_hash.uid
          identity.token = auth_hash.token
          identity.refresh_token = auth_hash.refresh_token
          identity.expires_at = Time.at(auth_hash.expires_at).utc.to_datetime if auth_hash.expires?

          identity
        end

        protected

        def find_by_uid_and_provider
          ::Atlassian::Identity.find_by_extern_uid(auth_hash.uid)&.user
        end

        def add_or_update_user_identities
          return unless gl_user

          identity = gl_user.atlassian_identity || gl_user.build_atlassian_identity
          self.class.assign_identity_from_auth_hash!(identity, auth_hash)
        end

        def auth_hash=(auth_hash)
          @auth_hash = ::Gitlab::Auth::Atlassian::AuthHash.new(auth_hash)
        end
      end
    end
  end
end