pad_sec 0.1.0.pre1 → 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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 30b3ef44aaa1d6ae3990d01136c4f23875ca03a7
4
- data.tar.gz: 0a07459a32fa42cacf32a6058972587bbde10832
3
+ metadata.gz: c528f7c1eeec8b88033fdf571cd6c4f3218c84a3
4
+ data.tar.gz: 4cfe641c1ea467b5d37aea0789b5784603e99baf
5
5
  SHA512:
6
- metadata.gz: 80d94f2ba40c4b3eb169b9ca4ae3fe6afcc6f75fc474c092c81e2efcd160230356380295c878d7ddea3b4ebdde195816708cf2f932680231c66cb8d8fb442449
7
- data.tar.gz: 294ff45b0e376f3570186c22f8f638d988fd99b99bad8ae29ac13e66261e55795585d97b708fc76995815017c19ad338c6e91505577e1011085942e93b594b12
6
+ metadata.gz: 84cab7a714450c4a06679a6d2dcfa30af37e223b3db13a826c089eb0a491f12523178a399beba4540dd2a41b0f2460aeba7e2bf1290b2007338ba813cf111a54
7
+ data.tar.gz: f7c72ef63f85f2d688d017f2d0b2294b8d4df4aa92c5c4b81fa7ccfc66693b4ea09bf0422d0bc17582b6ae9f688afc64a0b7d7391bdefc517536cf66815c9bec
@@ -0,0 +1,61 @@
1
+ module PadSec
2
+ module ConfigFile
3
+
4
+ # Gets the config file path.
5
+ #
6
+ # Based on `ENV['PADSTONE']`, gets the path to the config file.
7
+ #
8
+ # @return [String]
9
+ def self.path
10
+ if ENV['PADSTONE'] == 'development'
11
+ "results/.padstone/account"
12
+ else
13
+ "#{ENV['HOME']}/.padstone/account"
14
+ end
15
+ end
16
+
17
+ # Creates or updates the config file.
18
+ #
19
+ # @note A `username` and `pwd` are optional if a config file already exists.
20
+ #
21
+ # @param username [String]
22
+ # @param pwd [String]
23
+ # @param token [String]
24
+ # @return [Void] nothing
25
+ def self.write_config(username: nil, pwd: nil, token: nil)
26
+ hash = {}
27
+ config_file = PadSec::ConfigFile::path
28
+
29
+ # Check if there's already a config
30
+ if PadUtils.file_exist? config_file
31
+ # Yes. Get the username and pwd from it if not given in the params
32
+ config = PadUtils.json_file_to_hash(config_file)
33
+
34
+ if username.nil? && !config[:username].nil?
35
+ hash[:username] = config[:username]
36
+ else
37
+ hash[:username] = username
38
+ end
39
+
40
+ if pwd.nil? && !config[:pwd].nil?
41
+ hash[:pwd] = config[:pwd]
42
+ else
43
+ hash[:pwd] = pwd
44
+ end
45
+ else
46
+ # No. Get the values from the params.
47
+ hash = {
48
+ username: username,
49
+ pwd: pwd
50
+ }
51
+ end
52
+
53
+ # The token is always coming from the params.
54
+ hash[:token] = token
55
+
56
+ # Creates or overwrites the file
57
+ PadUtils.hash_to_json_file(config_file, hash)
58
+ end
59
+
60
+ end
61
+ end
@@ -0,0 +1,9 @@
1
+ module PadSec
2
+ module Encryption
3
+
4
+ def self.encrypt_password(pwd)
5
+ PadUtils.encrypt(content: pwd, key: PadSec::key)
6
+ end
7
+
8
+ end
9
+ end
@@ -0,0 +1,11 @@
1
+ module PadSec
2
+
3
+ def self.key
4
+ if ENV['PADSTONE'] == 'development'
5
+ "dev_key"
6
+ else
7
+ "b528d946-e5b9-49d2-b165-24e77187e90a"
8
+ end
9
+ end
10
+
11
+ end
@@ -0,0 +1,16 @@
1
+ module PadSec
2
+
3
+ # Gets the Padstone server url.
4
+ #
5
+ # Based on `ENV['PADSTONE']`, gets the Padstone server url.
6
+ #
7
+ # @return [String]
8
+ def self.server_url
9
+ if ENV['PADSTONE'] == 'development'
10
+ "http://localhost:3000/services/v1"
11
+ else
12
+ "http://padstone.io/services/v1"
13
+ end
14
+ end
15
+
16
+ end
@@ -1,3 +1,4 @@
1
1
  module PadSec
2
- VERSION = "0.1.0.pre1"
2
+ # Gem version
3
+ VERSION = "0.1.0"
3
4
  end
data/lib/pad_sec.rb CHANGED
@@ -1,9 +1,135 @@
1
1
  require_relative "pad_sec/version"
2
+ require_relative "pad_sec/config_file"
3
+ require_relative "pad_sec/encryption"
4
+ require_relative "pad_sec/keys"
5
+ require_relative "pad_sec/server"
2
6
 
3
7
  module PadSec
4
- # Your code goes here...
5
8
 
6
9
  def self.main(arg)
7
10
  puts "PadSec: #{arg}"
8
11
  end
12
+
13
+
14
+ # Authenticates a user
15
+ #
16
+ # @note If a `username` and `pwd` are given, `token` is optional and
17
+ # vice-versa.
18
+ # @param username [String]
19
+ # @param pwd [String]
20
+ # @param token [String]
21
+ # @return [Boolean]
22
+ # @example
23
+ # PadSec.authenticate(username: "Bob", pwd: "1234") # => true
24
+ # PadSec.authenticate(token: "f3dtd946-e5b9-46d1-b165-24ea32nm7e90a") # => false
25
+ def self.authenticate(username: nil, pwd: nil, token: nil)
26
+ if !token.nil?
27
+ # A token is given
28
+ authenticate_with_token(token: token)
29
+ elsif !username.nil? && !pwd.nil?
30
+ # A username and a password are given
31
+ authenticate_with_username(username: username, pwd: pwd)
32
+ elsif username.nil? && pwd.nil? && token.nil?
33
+ # Nothing is given
34
+ authenticate_from_file
35
+ else
36
+ # Incomplete params are given, such as only a password
37
+ false
38
+ end
39
+ end
40
+
41
+
42
+ # Checks if user is authenticated.
43
+ #
44
+ # @return [Boolean]
45
+ def self.authenticated?
46
+ authenticate
47
+ end
48
+
49
+ private
50
+
51
+
52
+ # Tries to authenticate from config file.
53
+ #
54
+ # PadSec will try to find the `.padstone/config` file and
55
+ # get `username` and `pwd` (or `token`) from it.
56
+ #
57
+ # @return [Boolean]
58
+ def self.authenticate_from_file
59
+ authenticated = false
60
+
61
+ config_file = PadSec::ConfigFile::path
62
+
63
+ # Does the config file exist?
64
+ if PadUtils.file_exist? config_file
65
+ # Yes. Parse it.
66
+ config = PadUtils.json_file_to_hash(config_file)
67
+
68
+ if !config[:token].nil?
69
+ # Attempt to authenticate with token
70
+ authenticated = authenticate_with_token(token: config[:token])
71
+ elsif !config[:username].nil? && !config[:pwd].nil?
72
+ # Attempt to authenticate with username, password
73
+ key = PadSec::key
74
+ de_pwd = PadUtils.decrypt(content: config[:pwd], key: key)
75
+ authenticated = authenticate_with_username(username: config[:username], pwd: de_pwd)
76
+ end
77
+ end
78
+
79
+ authenticated
80
+
81
+ end
82
+
83
+
84
+ # Tries to authenticate with a token.
85
+ #
86
+ # @param token [String]
87
+ # @return [Boolean]
88
+ # @example
89
+ # PadSec.authenticate_with_token(token: "f3dtd946-e5b9-46d1-b165-24ea32nm7e90a") # => true
90
+ def self.authenticate_with_token(token: nil)
91
+ return false if token.nil?
92
+
93
+ authenticated = false
94
+
95
+ body = {token: token}
96
+ server = PadSec::server_url
97
+ url = "#{server}/auth/authenticate"
98
+
99
+ reply = PadUtils.http_post(url: url, body: body)
100
+ if reply[:message] == "authenticated"
101
+ ConfigFile.write_config(token: reply[:token])
102
+ authenticated = true
103
+ end
104
+
105
+ authenticated
106
+
107
+ end
108
+
109
+
110
+ # Tries to authenticate with a username and password.
111
+ #
112
+ # @param username [String]
113
+ # @param password [String]
114
+ # @return [Boolean]
115
+ # @example
116
+ # PadSec.authenticate_with_username(username: "Bob", pwd: "1234") # => true
117
+ def self.authenticate_with_username(username: nil, pwd: nil)
118
+ enc_pwd = Encryption.encrypt_password(pwd)
119
+ body = {username: username, pwd: enc_pwd}
120
+ server = PadSec::server_url
121
+ url = "#{server}/auth/authenticate"
122
+
123
+ reply = PadUtils.http_post(url: url, body: body)
124
+
125
+ authenticated = false;
126
+ if reply[:message] == "authenticated"
127
+ ConfigFile.write_config(username: username, pwd: enc_pwd, token: reply[:token])
128
+ authenticated = true
129
+ end
130
+
131
+ authenticated
132
+
133
+ end
134
+
9
135
  end
metadata CHANGED
@@ -1,15 +1,35 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pad_sec
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0.pre1
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nico Schuele
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-03-04 00:00:00.000000000 Z
12
- dependencies: []
11
+ date: 2016-03-19 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: pad_utils
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '1.10'
20
+ - - "<"
21
+ - !ruby/object:Gem::Version
22
+ version: '2.0'
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ version: '1.10'
30
+ - - "<"
31
+ - !ruby/object:Gem::Version
32
+ version: '2.0'
13
33
  description: PadSec is the security module used by Padstone.
14
34
  email:
15
35
  - help@padstone.io
@@ -23,6 +43,10 @@ files:
23
43
  - Rakefile
24
44
  - bin/padsec
25
45
  - lib/pad_sec.rb
46
+ - lib/pad_sec/config_file.rb
47
+ - lib/pad_sec/encryption.rb
48
+ - lib/pad_sec/keys.rb
49
+ - lib/pad_sec/server.rb
26
50
  - lib/pad_sec/version.rb
27
51
  homepage: http://padstone.io
28
52
  licenses:
@@ -39,9 +63,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
39
63
  version: 2.2.2
40
64
  required_rubygems_version: !ruby/object:Gem::Requirement
41
65
  requirements:
42
- - - ">"
66
+ - - ">="
43
67
  - !ruby/object:Gem::Version
44
- version: 1.3.1
68
+ version: '0'
45
69
  requirements: []
46
70
  rubyforge_project:
47
71
  rubygems_version: 2.5.1