connected accounts

This commit is contained in:
Celina Lopez
2024-09-25 15:47:51 -07:00
parent 3b3743d6b2
commit 6abdaa2830
20 changed files with 227 additions and 13 deletions

View File

@@ -6,7 +6,7 @@
# chart_type :string not null
# metadata :jsonb
# name :string not null
# status :integer default(0), not null
# status :integer default("installing"), not null
# created_at :datetime not null
# updated_at :datetime not null
# cluster_id :bigint not null

View File

@@ -7,7 +7,7 @@
# commit_sha :string not null
# git_sha :string
# repository_url :string
# status :integer default(0)
# status :integer default("in_progress")
# created_at :datetime not null
# updated_at :datetime not null
# project_id :bigint not null

View File

@@ -5,7 +5,7 @@
# id :bigint not null, primary key
# kubeconfig :jsonb not null
# name :string not null
# status :integer default(0), not null
# status :integer default("initializing"), not null
# created_at :datetime not null
# updated_at :datetime not null
# user_id :bigint not null

View File

@@ -0,0 +1,27 @@
# == Schema Information
#
# Table name: connected_providers
#
# id :bigint not null, primary key
# access_token :string
# access_token_secret :string
# auth :text
# expires_at :datetime
# owner_type :string
# provider :string
# refresh_token :string
# uid :string
# created_at :datetime not null
# updated_at :datetime not null
# owner_id :bigint
#
# Indexes
#
# index_connected_providers_on_owner (owner_type,owner_id)
#
class ConnectedProvider < ApplicationRecord
include Token
include Oauth
belongs_to :owner, polymorphic: true
end

View File

@@ -0,0 +1,37 @@
module ConnectedProvider::Oauth
extend ActiveSupport::Concern
included do
serialize :auth, coder: JSON
encrypts :access_token
encrypts :access_token_secret
# Scopes for each provider
Devise.omniauth_configs.each do |provider, _|
scope provider, -> { where(provider: provider) }
end
end
class_methods do
def for_auth(auth)
where(provider: auth.provider, uid: auth.uid).first
end
end
def provider_name
provider.humanize
end
def name
auth&.dig("info", "name")
end
def email
auth&.dig("info", "email")
end
def image_url
auth&.dig("info", "image") || GravatarHelper.gravatar_url_for(email)
end
end

View File

@@ -0,0 +1,47 @@
module ConnectedProvider::Token
extend ActiveSupport::Concern
def token
renew_token! if expired?
access_token
end
def expired?
expires_at? && expires_at <= 30.minutes.from_now
end
def renew_token!
new_token = current_token.refresh!
update(
access_token: new_token.token,
refresh_token: new_token.refresh_token,
expires_at: Time.at(new_token.expires_at)
)
end
private
def current_token
OAuth2::AccessToken.new(
strategy.client,
access_token,
refresh_token:
)
end
def credentials_for(provider)
{
private_key: ENV["OMNIAUTH_#{provider.to_s.upcase}_PRIVATE_KEY"],
public_key: ENV["OMNIAUTH_#{provider.to_s.upcase}_PUBLIC_KEY"]
}.compact
end
def strategy
provider_config = credentials_for(provider)
OmniAuth::Strategies.const_get(OmniAuth::Utils.camelize(provider).to_s).new(
nil,
provider_config[:public_key], # client id
provider_config[:private_key] # client secret
)
end
end

View File

@@ -3,7 +3,7 @@
# Table name: deployments
#
# id :bigint not null, primary key
# status :integer default(0), not null
# status :integer default("in_progress"), not null
# created_at :datetime not null
# updated_at :datetime not null
# build_id :bigint not null

View File

@@ -36,4 +36,18 @@ class User < ApplicationRecord
has_many :projects, through: :clusters
has_one :docker_hub_credential, dependent: :destroy
has_many :add_ons, through: :clusters
has_many :connected_providers, as: :owner, dependent: :destroy
def github_username
github_account.auth["info"]["nickname"]
end
def github_access_token
github_account.access_token
end
def github_account
@_github_account ||= connected_providers.find_by(provider: "github")
end
end

View File

@@ -0,0 +1,15 @@
class CreateConnectedProviders < ActiveRecord::Migration[7.2]
def change
create_table :connected_providers do |t|
t.references :owner, polymorphic: true, index: true
t.string :access_token
t.string :access_token_secret
t.text :auth
t.string :provider
t.string :refresh_token
t.string :uid
t.datetime :expires_at
t.timestamps
end
end
end

17
db/schema.rb generated
View File

@@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema[7.2].define(version: 2024_09_25_213906) do
ActiveRecord::Schema[7.2].define(version: 2024_09_25_222027) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
@@ -85,6 +85,21 @@ ActiveRecord::Schema[7.2].define(version: 2024_09_25_213906) do
t.index ["user_id"], name: "index_clusters_on_user_id"
end
create_table "connected_providers", force: :cascade do |t|
t.string "owner_type"
t.bigint "owner_id"
t.string "access_token"
t.string "access_token_secret"
t.text "auth"
t.string "provider"
t.string "refresh_token"
t.string "uid"
t.datetime "expires_at"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["owner_type", "owner_id"], name: "index_connected_providers_on_owner"
end
create_table "cron_schedules", force: :cascade do |t|
t.bigint "service_id", null: false
t.string "schedule", null: false

View File

@@ -6,7 +6,7 @@
# chart_type :string not null
# metadata :jsonb
# name :string not null
# status :integer default(0), not null
# status :integer default("installing"), not null
# created_at :datetime not null
# updated_at :datetime not null
# cluster_id :bigint not null

View File

@@ -7,7 +7,7 @@
# commit_sha :string not null
# git_sha :string
# repository_url :string
# status :integer default(0)
# status :integer default("in_progress")
# created_at :datetime not null
# updated_at :datetime not null
# project_id :bigint not null

View File

@@ -5,7 +5,7 @@
# id :bigint not null, primary key
# kubeconfig :jsonb not null
# name :string not null
# status :integer default(0), not null
# status :integer default("initializing"), not null
# created_at :datetime not null
# updated_at :datetime not null
# user_id :bigint not null

31
test/fixtures/connected_providers.yml vendored Normal file
View File

@@ -0,0 +1,31 @@
# == Schema Information
#
# Table name: connected_providers
#
# id :bigint not null, primary key
# access_token :string
# access_token_secret :string
# auth :text
# expires_at :datetime
# owner_type :string
# provider :string
# refresh_token :string
# uid :string
# created_at :datetime not null
# updated_at :datetime not null
# owner_id :bigint
#
# Indexes
#
# index_connected_providers_on_owner (owner_type,owner_id)
#
# This model initially had no columns defined. If you add columns to the
# model remove the "{}" from the fixture names and add the columns immediately
# below each fixture, per the syntax in the comments below
#
one: {}
# column: value
#
two: {}
# column: value

View File

@@ -3,7 +3,7 @@
# Table name: deployments
#
# id :bigint not null, primary key
# status :integer default(0), not null
# status :integer default("in_progress"), not null
# created_at :datetime not null
# updated_at :datetime not null
# build_id :bigint not null

View File

@@ -6,7 +6,7 @@
# chart_type :string not null
# metadata :jsonb
# name :string not null
# status :integer default(0), not null
# status :integer default("installing"), not null
# created_at :datetime not null
# updated_at :datetime not null
# cluster_id :bigint not null

View File

@@ -7,7 +7,7 @@
# commit_sha :string not null
# git_sha :string
# repository_url :string
# status :integer default(0)
# status :integer default("in_progress")
# created_at :datetime not null
# updated_at :datetime not null
# project_id :bigint not null

View File

@@ -5,7 +5,7 @@
# id :bigint not null, primary key
# kubeconfig :jsonb not null
# name :string not null
# status :integer default(0), not null
# status :integer default("initializing"), not null
# created_at :datetime not null
# updated_at :datetime not null
# user_id :bigint not null

View File

@@ -0,0 +1,28 @@
# == Schema Information
#
# Table name: connected_providers
#
# id :bigint not null, primary key
# access_token :string
# access_token_secret :string
# auth :text
# expires_at :datetime
# owner_type :string
# provider :string
# refresh_token :string
# uid :string
# created_at :datetime not null
# updated_at :datetime not null
# owner_id :bigint
#
# Indexes
#
# index_connected_providers_on_owner (owner_type,owner_id)
#
require "test_helper"
class ConnectedProviderTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end
end

View File

@@ -3,7 +3,7 @@
# Table name: deployments
#
# id :bigint not null, primary key
# status :integer default(0), not null
# status :integer default("in_progress"), not null
# created_at :datetime not null
# updated_at :datetime not null
# build_id :bigint not null