From d64daadd216eb312f8025a99718f7b3c4747e57f Mon Sep 17 00:00:00 2001 From: Janis4411 Date: Tue, 2 Aug 2022 13:57:44 +0200 Subject: [PATCH] added AuthenticationToken model, updated some restrictions for the authentication token table, added dependent destroy to the user model for authentication tokens --- app/models/authentication_token.rb | 14 ++++++++++++++ app/models/user.rb | 1 + .../20220721131946_create_authentication_tokens.rb | 12 ++++++++++++ db/schema.rb | 13 ++++++++++++- 4 files changed, 39 insertions(+), 1 deletion(-) create mode 100644 app/models/authentication_token.rb create mode 100644 db/migrate/20220721131946_create_authentication_tokens.rb diff --git a/app/models/authentication_token.rb b/app/models/authentication_token.rb new file mode 100644 index 00000000..b1bda282 --- /dev/null +++ b/app/models/authentication_token.rb @@ -0,0 +1,14 @@ +# frozen_string_literal: true + +require 'securerandom' + +class AuthenticationToken < ApplicationRecord + include Creation + def self.generate!(user) + create!( + shared_secret: SecureRandom.hex(32), + user: user, + expire_at: 7.days.from_now + ) + end +end diff --git a/app/models/user.rb b/app/models/user.rb index 835a1617..cd5e5379 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -6,6 +6,7 @@ class User < ApplicationRecord ROLES = %w[admin teacher learner].freeze belongs_to :consumer + has_many :authentication_token, dependent: :destroy has_many :study_group_memberships, as: :user has_many :study_groups, through: :study_group_memberships, as: :user has_many :exercises, as: :user diff --git a/db/migrate/20220721131946_create_authentication_tokens.rb b/db/migrate/20220721131946_create_authentication_tokens.rb new file mode 100644 index 00000000..6cf1208d --- /dev/null +++ b/db/migrate/20220721131946_create_authentication_tokens.rb @@ -0,0 +1,12 @@ +# frozen_string_literal: true + +class CreateAuthenticationTokens < ActiveRecord::Migration[6.1] + def change + create_table :authentication_tokens, id: :uuid do |t| + t.string :shared_secret, null: false, index: {unique: true} + t.references :user, polymorphic: true, null: false + t.date :expire_at, null: false + t.timestamps + end + end +end diff --git a/db/schema.rb b/db/schema.rb index 141de618..4a9ca522 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 2022_04_15_215112) do +ActiveRecord::Schema.define(version: 2022_07_21_131946) do # These are extensions that must be enabled in order to support this database enable_extension "pg_trgm" @@ -30,6 +30,17 @@ ActiveRecord::Schema.define(version: 2022_04_15_215112) do t.index ["user_type", "user_id"], name: "index_anomaly_notifications_on_user" end + create_table "authentication_tokens", id: :uuid, default: -> { "gen_random_uuid()" }, force: :cascade do |t| + t.string "shared_secret", null: false + t.string "user_type", null: false + t.bigint "user_id", null: false + t.date "expire_at", null: false + t.datetime "created_at", precision: 6, null: false + t.datetime "updated_at", precision: 6, null: false + t.index ["shared_secret"], name: "index_authentication_tokens_on_shared_secret", unique: true + t.index ["user_type", "user_id"], name: "index_authentication_tokens_on_user" + end + create_table "codeharbor_links", id: :serial, force: :cascade do |t| t.string "api_key" t.datetime "created_at"