akamai-edgeauth 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.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/akamai/edgeauth.rb +199 -0
  3. metadata +44 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 83c736a708b2fb20a392c036e30bcd54ca622190
4
+ data.tar.gz: aa2f7d5c9b23ce48c09428afc23eedf9fa183b4a
5
+ SHA512:
6
+ metadata.gz: c22efda1440facf518e3f6ae61409b10ae6b52f23d5724acf07cdd35d1d0601c6a2424e8faa4d4f772ad78172c5bdec151e0fea3ad019de2070d3369bef8825e
7
+ data.tar.gz: 15dea57f6d38d873b3c5e7b30e243f8f543ea220bebd3cce3e9e8a73ea22bc231dbfc3de1f3db7612aa7b07c7a78ea6e1e376ab3d67666ad2606d1c4f22e92a1
@@ -0,0 +1,199 @@
1
+ # Copyright 2017 Akamai Technologies http://developer.akamai.com.
2
+
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+
16
+ require 'cgi'
17
+ require 'openssl'
18
+ require 'optparse'
19
+
20
+
21
+ ENV['TZ'] = 'GMT'
22
+
23
+
24
+ module Akamai
25
+ class EdgeAuthError < Exception
26
+ """Base-class for all exceptions raised by EdgeAuth Class"""
27
+ end
28
+
29
+
30
+ class EdgeAuth
31
+ attr_accessor :token_type, :token_name, :key, :algorithm, :salt,
32
+ :start_time, :end_time, :window_secondse, :field_delimiter,
33
+ :acl_delimiter, :escape_early, :verbose
34
+
35
+ @@acl_delimiter = '!'
36
+ def self.ACL_DELIMITER
37
+ @@acl_delimiter
38
+ end
39
+
40
+ def initialize(token_type: nil, token_name: '__token__', key: nil,
41
+ algorithm: 'sha256', salt: nil, start_time: nil, end_time: nil,
42
+ window_seconds: nil, field_delimiter: '~', escape_early: false, verbose: false)
43
+
44
+ @token_type = token_type
45
+ @token_name = token_name
46
+ @start_time = start_time
47
+ @end_time = end_time
48
+ @window_seconds = window_seconds
49
+ if !key || key.length <= 0
50
+ raise EdgeAuthError,
51
+ 'You must provide a secret in order to generate a new token.'
52
+ end
53
+ @key = key
54
+ @algorithm = algorithm
55
+ @salt = salt
56
+ @field_delimiter = field_delimiter
57
+ @escape_early = escape_early
58
+ @verbose = verbose
59
+ end
60
+
61
+ def _escapeEarly(text)
62
+ if @escape_early
63
+ return CGI::escape(text).gsub(/(%..)/) {$1.downcase}
64
+ else
65
+ return text
66
+ end
67
+ end
68
+
69
+ def generateToken(url: nil, acl: nil, start_time: nil, end_time: nil, window_seconds: nil,
70
+ ip: nil, payload: nil, session_id: nil)
71
+
72
+ if !start_time
73
+ start_time = @start_time
74
+ end
75
+ if !end_time
76
+ end_time = @end_time
77
+ end
78
+ if !window_seconds
79
+ window_seconds = @window_seconds
80
+ end
81
+
82
+ if start_time.to_s.downcase == 'now'
83
+ start_time = Time.new.getgm.to_i
84
+ elsif start_time
85
+ begin
86
+ if start_time <= 0
87
+ raise EdgeAuthError, 'start_time must be ( > 0 )'
88
+ end
89
+ rescue
90
+ raise EdgeAuthError, 'start_time must be numeric or now'
91
+ end
92
+
93
+ end
94
+
95
+ if end_time
96
+ begin
97
+ if end_time <= 0
98
+ raise EdgeAuthError, 'end_time must be ( > 0 )'
99
+ end
100
+ rescue
101
+ raise EdgeAuthError, 'end_time must be numeric'
102
+ end
103
+ end
104
+
105
+ if window_seconds
106
+ begin
107
+ if window_seconds <= 0
108
+ raise EdgeAuthError, 'window_seconds must be ( > 0 )'
109
+ end
110
+ rescue
111
+ raise EdgeAuthError, 'window_seconds must be numeric'
112
+ end
113
+ end
114
+
115
+ if !end_time
116
+ if window_seconds
117
+ if !start_time
118
+ end_time = Time.new.getgm.to_i + window_seconds
119
+ else
120
+ end_time = start_time + window_seconds
121
+ end
122
+ else
123
+ raise EdgeAuthError, 'You must provide an expiration time or a duration window..'
124
+ end
125
+ end
126
+
127
+ if start_time && end_time <= start_time
128
+ raise EdgeAuthError, 'Token will have already expired.'
129
+ end
130
+
131
+ if (!acl && !url) || (acl && url)
132
+ raise EdgeAuthError, 'You must provide a URL or an ACL'
133
+ end
134
+
135
+ if @verbose
136
+ puts "Akamai Token Generation Parameters"
137
+ puts "Token Type : #{@token_type}"
138
+ puts "Token Name : #{@token_name}"
139
+ puts "Start Time : #{start_time}"
140
+ puts "End Time : #{end_time}"
141
+ puts "Window(seconds) : #{window_seconds}"
142
+ puts "IP : #{ip}"
143
+ puts "URL : #{url}"
144
+ puts "ACL : #{acl}"
145
+ puts "Key/Secret : #{@key}"
146
+ puts "Payload : #{payload}"
147
+ puts "Algo : #{@algo}"
148
+ puts "Salt : #{@salt}"
149
+ puts "Session ID : #{session_id}"
150
+ puts "Field Delimiter : #{@field_delimiter}"
151
+ puts "ACL Delimiter : #{@@acl_delimiter}"
152
+ puts "Escape Early : #{@escape_early}"
153
+ end
154
+
155
+ hash_code = Array.new
156
+ new_token = Array.new
157
+
158
+ if ip
159
+ new_token.push('ip=%s' % _escapeEarly(ip))
160
+ end
161
+ if start_time
162
+ new_token.push('st=%s' % start_time)
163
+ end
164
+ new_token.push('exp=%s' % end_time)
165
+
166
+ if acl
167
+ new_token.push('acl=%s' % acl)
168
+ end
169
+ if session_id
170
+ new_token.push('id=%s' % _escapeEarly(session_id))
171
+ end
172
+ if payload
173
+ new_token.push('data=%s' % _escapeEarly(payload))
174
+ end
175
+
176
+ hash_code = new_token.clone
177
+
178
+ if url and !acl
179
+ hash_code.push('url=%s' % _escapeEarly(url))
180
+ end
181
+
182
+ if @salt
183
+ hash_code.push('salt=%s' % @salt)
184
+ end
185
+ if !(['sha256', 'sha1', 'md5'].include? @algorithm)
186
+ raise EdgeAuthError, 'Unknown algorithm'
187
+ end
188
+
189
+ bin_key = Array(@key.gsub(/\s/,'')).pack("H*")
190
+ digest = OpenSSL::Digest.new(@algorithm)
191
+ token_hmac = OpenSSL::HMAC.new(bin_key, digest)
192
+ token_hmac.update(hash_code.join(@field_delimiter))
193
+
194
+ new_token.push('hmac=%s' % token_hmac)
195
+
196
+ return new_token.join(@field_delimiter)
197
+ end
198
+ end
199
+ end
metadata ADDED
@@ -0,0 +1,44 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: akamai-edgeauth
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Astin Choi
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-02-22 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Akamai-EdgeAuth is Akamai Edge Authorization Token for Ruby 2.0+
14
+ email: achoi@akamai.com
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - lib/akamai/edgeauth.rb
20
+ homepage: https://github.com/AstinCHOI/AkamaiEdgeAuth-Ruby
21
+ licenses:
22
+ - Apache-2.0
23
+ metadata: {}
24
+ post_install_message:
25
+ rdoc_options: []
26
+ require_paths:
27
+ - lib
28
+ required_ruby_version: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: '2.0'
33
+ required_rubygems_version: !ruby/object:Gem::Requirement
34
+ requirements:
35
+ - - ">="
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ requirements: []
39
+ rubyforge_project:
40
+ rubygems_version: 2.5.2
41
+ signing_key:
42
+ specification_version: 4
43
+ summary: Akamai Edge Authorization Token for Ruby
44
+ test_files: []