telegram-auth 0.0.2 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/telegram/auth.rb +40 -0
- data/lib/telegram/auth/configuration.rb +29 -0
- data/lib/telegram/auth/fields.rb +22 -0
- data/lib/telegram/auth/verification.rb +27 -0
- data/lib/telegram/auth/version.rb +5 -0
- metadata +23 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 82653d76cf5a5ce000ce009ebaccab8fa57ec32b6c3ec848bcb440bb3cdbf4f6
|
4
|
+
data.tar.gz: 723190f3a810b0bf68c3887bb5e419fed47fc598107dee2bf6620ae8ba4f5f61
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c3e5f89e314b7088134746aaeda7a0110cb8301fd3ddd2ea52fc3e9cc2eb0f8c1db5a45308cb440d21871a9cab6c7a989f0c360f5ed49910fd1ee26035bf80bc
|
7
|
+
data.tar.gz: b917711aeee692453f8f4688f12cf6add3bf4730326d8e90804ecb1aa716684e5d143fde97cbd9c0009c853283b116de329d73f54290b31c08501d7ef1a135bf
|
@@ -0,0 +1,40 @@
|
|
1
|
+
require "telegram/auth/version"
|
2
|
+
require "telegram/auth/configuration"
|
3
|
+
require "telegram/auth/fields"
|
4
|
+
require "telegram/auth/verification"
|
5
|
+
require "logger"
|
6
|
+
|
7
|
+
module Telegram
|
8
|
+
module Auth
|
9
|
+
class Error < StandardError; end
|
10
|
+
class ConfigurationError < Error; end
|
11
|
+
class ShaError < Error; end;
|
12
|
+
class ExpiredError < Error; end;
|
13
|
+
def self.configure(&block)
|
14
|
+
yield(Configuration.instance) if block_given?
|
15
|
+
end
|
16
|
+
|
17
|
+
def self.configure!(&blk)
|
18
|
+
configure(&blk)
|
19
|
+
Configuration.instance.verify!
|
20
|
+
end
|
21
|
+
|
22
|
+
def self.create(hash:, **field_data)
|
23
|
+
verification = Verification.new(hash, Fields.new(field_data))
|
24
|
+
success = verification.process
|
25
|
+
yield(verification.error) if !success && block_given?
|
26
|
+
success
|
27
|
+
end
|
28
|
+
|
29
|
+
def self.logger=(logger)
|
30
|
+
@logger = logger
|
31
|
+
end
|
32
|
+
|
33
|
+
def self.logger
|
34
|
+
@logger
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
Telegram::Auth.logger = Logger.new(STDOUT)
|
40
|
+
Telegram::Auth.logger.level = Logger::DEBUG
|
@@ -0,0 +1,29 @@
|
|
1
|
+
require "singleton"
|
2
|
+
|
3
|
+
module Telegram::Auth
|
4
|
+
class Configuration
|
5
|
+
include Singleton
|
6
|
+
attr_accessor :token
|
7
|
+
attr_accessor :auth_expires_in
|
8
|
+
|
9
|
+
def valid?
|
10
|
+
@errors = []
|
11
|
+
@errors << ConfigurationError.new("Invalid token") if (!token || token.empty?)
|
12
|
+
@errors.none?
|
13
|
+
end
|
14
|
+
|
15
|
+
def validate!
|
16
|
+
valid? or raise @errors.first
|
17
|
+
end
|
18
|
+
|
19
|
+
def verify!
|
20
|
+
return true if valid?
|
21
|
+
raise @errors.shift
|
22
|
+
end
|
23
|
+
|
24
|
+
def reset!
|
25
|
+
self.token = nil
|
26
|
+
self.auth_expires_in = nil
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
module Telegram::Auth
|
2
|
+
class Fields
|
3
|
+
def initialize(hash)
|
4
|
+
@hash = hash
|
5
|
+
end
|
6
|
+
|
7
|
+
def expired?(time = Time.now.to_i)
|
8
|
+
return false unless Configuration.instance.auth_expires_in.present?
|
9
|
+
time > @hash[:auth_date] + Configuration.instance.auth_expires_in
|
10
|
+
end
|
11
|
+
|
12
|
+
def hash
|
13
|
+
token_sha = OpenSSL::Digest::SHA256.new.digest(Configuration.instance.token)
|
14
|
+
OpenSSL::HMAC.hexdigest(OpenSSL::Digest::SHA256.new, token_sha, to_s)
|
15
|
+
end
|
16
|
+
|
17
|
+
def to_s
|
18
|
+
permitted_hash = @hash.slice(:auth_date, :id, :username, :first_name, :last_name, :photo_url)
|
19
|
+
permitted_hash.map { |k,v| "#{k}=#{v}" }.sort.join("\n")
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
require 'openssl'
|
2
|
+
|
3
|
+
module Telegram::Auth
|
4
|
+
class Verification
|
5
|
+
attr_reader :error
|
6
|
+
|
7
|
+
def initialize(hash, fields)
|
8
|
+
@fields = fields
|
9
|
+
@hash = hash
|
10
|
+
end
|
11
|
+
|
12
|
+
def process
|
13
|
+
Configuration.instance.validate! && check_sha && check_expiry
|
14
|
+
end
|
15
|
+
|
16
|
+
private
|
17
|
+
def check_sha
|
18
|
+
@error = ShaError.new("Invalid hash") unless @hash.casecmp(@fields.hash) == 0
|
19
|
+
!@error
|
20
|
+
end
|
21
|
+
|
22
|
+
def check_expiry
|
23
|
+
@error = ExpiredError.new("Expired") if @fields.expired?
|
24
|
+
!@error
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: telegram-auth
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Aninda
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2020-10-
|
11
|
+
date: 2020-10-25 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -80,13 +80,32 @@ dependencies:
|
|
80
80
|
- - "~>"
|
81
81
|
- !ruby/object:Gem::Version
|
82
82
|
version: 0.9.2
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: openssl
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - "~>"
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: 2.2.0
|
90
|
+
type: :runtime
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - "~>"
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: 2.2.0
|
83
97
|
description: Gem to verify the auth hash for a telegram domain login
|
84
98
|
email:
|
85
99
|
- spacefugitive@gmail.com
|
86
100
|
executables: []
|
87
101
|
extensions: []
|
88
102
|
extra_rdoc_files: []
|
89
|
-
files:
|
103
|
+
files:
|
104
|
+
- lib/telegram/auth.rb
|
105
|
+
- lib/telegram/auth/configuration.rb
|
106
|
+
- lib/telegram/auth/fields.rb
|
107
|
+
- lib/telegram/auth/verification.rb
|
108
|
+
- lib/telegram/auth/version.rb
|
90
109
|
homepage: https://github.com/spacefugitive/telegram-auth
|
91
110
|
licenses:
|
92
111
|
- MIT
|
@@ -99,7 +118,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
99
118
|
requirements:
|
100
119
|
- - ">="
|
101
120
|
- !ruby/object:Gem::Version
|
102
|
-
version:
|
121
|
+
version: 2.4.0
|
103
122
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
104
123
|
requirements:
|
105
124
|
- - ">="
|