cli_login_engine 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/README.md +47 -0
- data/Rakefile +8 -0
- data/app/controllers/cli_login_engine/application_controller.rb +4 -0
- data/app/controllers/cli_login_engine/one_time_logins_controller.rb +35 -0
- data/app/models/cli_login_engine/application_record.rb +5 -0
- data/app/models/cli_login_engine/login_token.rb +24 -0
- data/config/routes.rb +3 -0
- data/db/migrate/20250607081232_create_cli_login_engine_login_tokens.rb +11 -0
- data/lib/cli_login_engine/engine.rb +5 -0
- data/lib/cli_login_engine/version.rb +3 -0
- data/lib/cli_login_engine.rb +6 -0
- data/lib/tasks/login_token.rake +32 -0
- metadata +86 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: a059d3d24f17220288c325694191a52414e953eb4c134c20be57b7976235f7f3
|
4
|
+
data.tar.gz: cc77d7432f1f496a4f64be3140bb8baee8826e79f8fb0b9cfa6196087f43cb16
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 31dff0fdeff3ede74e22804c6c1c9a2779fff4e8e36735a3f18a81fb41c99c639abaa5d1fbe2d82791deae1908520e6a4b2b787c6eecf5a8bef9ee23b75d6bd9
|
7
|
+
data.tar.gz: 49ac73954d01823d9935f199ffdfbe489848a2fd3f8080a08ca765a1ae9461bf743531feffdb1c6cab5bcaf4cdac0e56b2cac54f8f760eb4de3a855d12b79ee4
|
data/README.md
ADDED
@@ -0,0 +1,47 @@
|
|
1
|
+
# CliLoginEngine
|
2
|
+
Short description and motivation.
|
3
|
+
|
4
|
+
## Usage
|
5
|
+
|
6
|
+
Environment Variables
|
7
|
+
|
8
|
+
```.env
|
9
|
+
CLI_LOGIN_ENGINE_HOST=example.com
|
10
|
+
CLI_LOGIN_REDIRECT_PATH=/dashboard
|
11
|
+
```
|
12
|
+
|
13
|
+
Generate a One-Time Login Token
|
14
|
+
|
15
|
+
```bash
|
16
|
+
rake 'cli_login_engine:generate_token[user@example.com]'
|
17
|
+
```
|
18
|
+
|
19
|
+
Example output:
|
20
|
+
|
21
|
+
```bash
|
22
|
+
Generated login URL:
|
23
|
+
http://localhost:3000/cli_login_engine/one_time_logins/xxxx
|
24
|
+
```
|
25
|
+
|
26
|
+
## Installation
|
27
|
+
Add this line to your application's Gemfile:
|
28
|
+
|
29
|
+
```ruby
|
30
|
+
gem "cli_login_engine"
|
31
|
+
```
|
32
|
+
|
33
|
+
And then execute:
|
34
|
+
```bash
|
35
|
+
$ bundle
|
36
|
+
```
|
37
|
+
|
38
|
+
Or install it yourself as:
|
39
|
+
```bash
|
40
|
+
$ gem install cli_login_engine
|
41
|
+
```
|
42
|
+
|
43
|
+
## Contributing
|
44
|
+
Contribution directions go here.
|
45
|
+
|
46
|
+
## License
|
47
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
module CliLoginEngine
|
2
|
+
class OneTimeLoginsController < ApplicationController
|
3
|
+
def show
|
4
|
+
# すでにログイン済みならそのままリダイレクト
|
5
|
+
if user_signed_in?
|
6
|
+
redirect_to ENV.fetch("CLI_LOGIN_REDIRECT_PATH", "/admin")
|
7
|
+
return
|
8
|
+
end
|
9
|
+
|
10
|
+
input_token = params[:token]
|
11
|
+
begin
|
12
|
+
login_token = CliLoginEngine::LoginToken.find_by_raw_token(input_token)
|
13
|
+
rescue ActiveRecord::RecordNotFound
|
14
|
+
# ログイントークンが存在しない場合はエラーを返す
|
15
|
+
render plain: "Invalid token", status: :unauthorized
|
16
|
+
return
|
17
|
+
end
|
18
|
+
# トークンの有効期限を確認
|
19
|
+
if login_token.expired?
|
20
|
+
render plain: "Token expired", status: :unauthorized
|
21
|
+
return
|
22
|
+
end
|
23
|
+
|
24
|
+
if login_token
|
25
|
+
# Devise の sign_in を利用
|
26
|
+
sign_in(login_token.user)
|
27
|
+
login_token.destroy
|
28
|
+
redirect_path = ENV.fetch("CLI_LOGIN_REDIRECT_PATH", "/admin")
|
29
|
+
redirect_to redirect_path, notice: t("cli_login_engine.login_success")
|
30
|
+
else
|
31
|
+
render plain: "Invalid token", status: :unauthorized
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
module CliLoginEngine
|
2
|
+
class LoginToken < ApplicationRecord
|
3
|
+
belongs_to :user, class_name: "::User"
|
4
|
+
|
5
|
+
attr_reader :raw_token
|
6
|
+
|
7
|
+
before_create do
|
8
|
+
@raw_token = SecureRandom.urlsafe_base64(32)
|
9
|
+
self.token = Digest::MD5.hexdigest(@raw_token)
|
10
|
+
self.expires_at ||= 5.minutes.from_now
|
11
|
+
end
|
12
|
+
|
13
|
+
# 有効期限を過ぎているかどうかを確認する
|
14
|
+
def expired?
|
15
|
+
expires_at < Time.current
|
16
|
+
end
|
17
|
+
|
18
|
+
# トークン文字列から一致するレコードを探す
|
19
|
+
def self.find_by_raw_token(input_token)
|
20
|
+
hashed = Digest::MD5.hexdigest(input_token)
|
21
|
+
find_by!(token: hashed)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
data/config/routes.rb
ADDED
@@ -0,0 +1,11 @@
|
|
1
|
+
class CreateCliLoginEngineLoginTokens < ActiveRecord::Migration[8.0]
|
2
|
+
def change
|
3
|
+
create_table :cli_login_engine_login_tokens do |t|
|
4
|
+
t.references :user, null: false, foreign_key: true
|
5
|
+
t.string :token
|
6
|
+
t.datetime :expires_at
|
7
|
+
|
8
|
+
t.timestamps
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
namespace :cli_login_engine do
|
2
|
+
desc "Generate a one-time login token for a user by email"
|
3
|
+
task :generate_token, [ :email ] => :environment do |t, args|
|
4
|
+
email = args[:email]
|
5
|
+
|
6
|
+
if email.blank?
|
7
|
+
puts "Usage: rake 'cli_login_engine:generate_token[email@example.com]'"
|
8
|
+
exit 1
|
9
|
+
end
|
10
|
+
|
11
|
+
user = User.find_by(email: email)
|
12
|
+
if user.nil?
|
13
|
+
puts "User not found for email: #{email}"
|
14
|
+
exit 1
|
15
|
+
end
|
16
|
+
|
17
|
+
token = CliLoginEngine::LoginToken.create!(user: user)
|
18
|
+
|
19
|
+
# ルーティングヘルパーにアクセス
|
20
|
+
include CliLoginEngine::Engine.routes.url_helpers
|
21
|
+
|
22
|
+
host = ENV.fetch("CLI_LOGIN_ENGINE_HOST", "localhost:3000")
|
23
|
+
|
24
|
+
# ホストを指定
|
25
|
+
default_url_options[:host] = host
|
26
|
+
|
27
|
+
# エンジンのルートヘルパーを使って URL を生成
|
28
|
+
url = one_time_login_url(token: token.raw_token)
|
29
|
+
|
30
|
+
puts "Generated login URL:\n#{url}"
|
31
|
+
end
|
32
|
+
end
|
metadata
ADDED
@@ -0,0 +1,86 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: cli_login_engine
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- terao
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2025-06-07 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rails
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 8.0.2
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 8.0.2
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: devise
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '4.9'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '4.9'
|
41
|
+
description: The device provides user login functionality from commands similar to
|
42
|
+
drupal's drush uli.
|
43
|
+
email:
|
44
|
+
- ''
|
45
|
+
executables: []
|
46
|
+
extensions: []
|
47
|
+
extra_rdoc_files: []
|
48
|
+
files:
|
49
|
+
- README.md
|
50
|
+
- Rakefile
|
51
|
+
- app/controllers/cli_login_engine/application_controller.rb
|
52
|
+
- app/controllers/cli_login_engine/one_time_logins_controller.rb
|
53
|
+
- app/models/cli_login_engine/application_record.rb
|
54
|
+
- app/models/cli_login_engine/login_token.rb
|
55
|
+
- config/routes.rb
|
56
|
+
- db/migrate/20250607081232_create_cli_login_engine_login_tokens.rb
|
57
|
+
- lib/cli_login_engine.rb
|
58
|
+
- lib/cli_login_engine/engine.rb
|
59
|
+
- lib/cli_login_engine/version.rb
|
60
|
+
- lib/tasks/login_token.rake
|
61
|
+
homepage: https://github.com/teratai3/cli_login_engine
|
62
|
+
licenses: []
|
63
|
+
metadata:
|
64
|
+
homepage_uri: https://github.com/teratai3/cli_login_engine
|
65
|
+
source_code_uri: https://github.com/teratai3/cli_login_engine
|
66
|
+
changelog_uri: https://github.com/teratai3/cli_login_engine
|
67
|
+
post_install_message:
|
68
|
+
rdoc_options: []
|
69
|
+
require_paths:
|
70
|
+
- lib
|
71
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
77
|
+
requirements:
|
78
|
+
- - ">="
|
79
|
+
- !ruby/object:Gem::Version
|
80
|
+
version: '0'
|
81
|
+
requirements: []
|
82
|
+
rubygems_version: 3.4.19
|
83
|
+
signing_key:
|
84
|
+
specification_version: 4
|
85
|
+
summary: Alternate login for developers
|
86
|
+
test_files: []
|