iprog_otp_generator 0.1.3
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/lib/generators/install_generator.rb +28 -0
- data/lib/iprog_otp_generator/otp_code.rb +34 -0
- data/lib/iprog_otp_generator/version.rb +5 -0
- data/lib/iprog_otp_generator.rb +23 -0
- metadata +76 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: f2c23cb2f515b6f8ae9b63224f6f13ba50bb1c4bed6c4ffc4481a9809d197fe1
|
4
|
+
data.tar.gz: 9be3ba61bf549b1f317bf550011038e3e1044d18fecb27181da437911bd518be
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 548eadab8964b1f8963ef5a4ae7c0d41dba756abf7dcc16645776bc5cfd83e015e5e33e5eb8c55615fbdeefd6b8eca0313828b7c379c635d3b83488e88ac40fa
|
7
|
+
data.tar.gz: 27b55ff146f4654f7008a0be5381cecc6446e3cd76dde0c1dfa3ed6f086d0372f1332b5be842f4f907b3ffcbf97aa7f9de4b600db096065190f05bf9f6acfbdc
|
@@ -0,0 +1,28 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module IprogOtpGenerator
|
4
|
+
module Generators
|
5
|
+
class InstallGenerator < Rails::Generators::Base
|
6
|
+
source_root File.expand_path('..', __FILE__)
|
7
|
+
|
8
|
+
argument :model_name, type: :string, default: "User", banner: "MODEL"
|
9
|
+
|
10
|
+
desc "Creates a migration to add OTP fields to the specified model's table"
|
11
|
+
|
12
|
+
def copy_migration
|
13
|
+
model = model_name.constantize
|
14
|
+
table_name = model.table_name
|
15
|
+
timestamp = Time.now.utc.strftime("%Y%m%d%H%M%S")
|
16
|
+
|
17
|
+
migration_template "migration.rb.tt", "db/migrate/add_otp_fields_to#{table_name}.rb", migration_version: migration_version, table_name: table_name
|
18
|
+
end
|
19
|
+
|
20
|
+
private
|
21
|
+
def migration_version
|
22
|
+
if Rails.version.start_with?("5") || Rails.version.start_with("6")
|
23
|
+
"[#{Rails::VERSION::MAJOR}.#{Rails::VERSION::MINOR}]"
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module IprogOtpGenerator
|
4
|
+
module OtpCode
|
5
|
+
def generate_otp_code(length = 8)
|
6
|
+
begin
|
7
|
+
self.otp_code = Array.new(length) { rand(0..9) }.join
|
8
|
+
end while self.class.exists?(otp_code: otp_code)
|
9
|
+
|
10
|
+
self.otp_code_expires_at = Time.now + 5.minutes
|
11
|
+
self.otp_code_confirmed = false
|
12
|
+
|
13
|
+
save(validate: false)
|
14
|
+
end
|
15
|
+
|
16
|
+
def valid_otp_code?(code = nil)
|
17
|
+
return false unless otp_code && otp_code_expires_at
|
18
|
+
|
19
|
+
if otp_code == code && otp_code_expires_at > Time.now
|
20
|
+
update_columns otp_code_confirmed: true
|
21
|
+
end
|
22
|
+
|
23
|
+
otp_code_confirmed
|
24
|
+
end
|
25
|
+
|
26
|
+
def clear_otp_code
|
27
|
+
self.otp_code = nil
|
28
|
+
self.otp_code_expires_at = nil
|
29
|
+
self.otp_code_confirmed = false
|
30
|
+
|
31
|
+
save(validate: false)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "active_record"
|
4
|
+
require_relative "iprog_otp_generator/version"
|
5
|
+
require_relative "iprog_otp_generator/otp_code"
|
6
|
+
|
7
|
+
if defined?(Rails)
|
8
|
+
require_relative "iprog_otp_generator/generators/install_generator"
|
9
|
+
end
|
10
|
+
|
11
|
+
module IprogOtpGenerator
|
12
|
+
def self.included(base)
|
13
|
+
base.extend(ClassMethods)
|
14
|
+
end
|
15
|
+
|
16
|
+
module ClassMethods
|
17
|
+
def enable_otp
|
18
|
+
include IprogOtpGenerator::OtpCode
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
ActiveRecord::Base.include IprogOtpGenerator
|
metadata
ADDED
@@ -0,0 +1,76 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: iprog_otp_generator
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.3
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Jayson Presto
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2024-10-18 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: activerecord
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rspec
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
description: This gem allows ActiveRecord models to generate, validate, and clear
|
42
|
+
OTP codes with an expiration time.
|
43
|
+
email:
|
44
|
+
- jaysonpresto.iprog21@gmail.com
|
45
|
+
executables: []
|
46
|
+
extensions: []
|
47
|
+
extra_rdoc_files: []
|
48
|
+
files:
|
49
|
+
- lib/generators/install_generator.rb
|
50
|
+
- lib/iprog_otp_generator.rb
|
51
|
+
- lib/iprog_otp_generator/otp_code.rb
|
52
|
+
- lib/iprog_otp_generator/version.rb
|
53
|
+
homepage: https://github.com/iprog21/iprog_otp_generator
|
54
|
+
licenses:
|
55
|
+
- MIT
|
56
|
+
metadata: {}
|
57
|
+
post_install_message:
|
58
|
+
rdoc_options: []
|
59
|
+
require_paths:
|
60
|
+
- lib
|
61
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
62
|
+
requirements:
|
63
|
+
- - ">="
|
64
|
+
- !ruby/object:Gem::Version
|
65
|
+
version: '0'
|
66
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
67
|
+
requirements:
|
68
|
+
- - ">="
|
69
|
+
- !ruby/object:Gem::Version
|
70
|
+
version: '0'
|
71
|
+
requirements: []
|
72
|
+
rubygems_version: 3.5.21
|
73
|
+
signing_key:
|
74
|
+
specification_version: 4
|
75
|
+
summary: A simple OTP generator for ActiveRecord models
|
76
|
+
test_files: []
|