mirror of
https://github.com/czhu12/canine.git
synced 2025-12-19 09:49:58 -06:00
connected accounts
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
27
app/models/connected_provider.rb
Normal file
27
app/models/connected_provider.rb
Normal 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
|
||||
37
app/models/connected_provider/oauth.rb
Normal file
37
app/models/connected_provider/oauth.rb
Normal 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
|
||||
47
app/models/connected_provider/token.rb
Normal file
47
app/models/connected_provider/token.rb
Normal 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
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
15
db/migrate/20240925222027_create_connected_providers.rb
Normal file
15
db/migrate/20240925222027_create_connected_providers.rb
Normal 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
17
db/schema.rb
generated
@@ -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
|
||||
|
||||
2
test/fixtures/add_ons.yml
vendored
2
test/fixtures/add_ons.yml
vendored
@@ -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
|
||||
|
||||
2
test/fixtures/builds.yml
vendored
2
test/fixtures/builds.yml
vendored
@@ -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
|
||||
|
||||
2
test/fixtures/clusters.yml
vendored
2
test/fixtures/clusters.yml
vendored
@@ -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
31
test/fixtures/connected_providers.yml
vendored
Normal 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
|
||||
2
test/fixtures/deployments.yml
vendored
2
test/fixtures/deployments.yml
vendored
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
28
test/models/connected_provider_test.rb
Normal file
28
test/models/connected_provider_test.rb
Normal 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
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user