akamai-authtoken 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/akamai/authtoken.rb +161 -0
  3. metadata +44 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: c1d6b93d6b4ddac65963b2a51b60ad4c003f79a7
4
+ data.tar.gz: b0c1e74bd7c1b55ceb4f50ea31735a391b9125db
5
+ SHA512:
6
+ metadata.gz: 934edf4bc3495870db8fab8aa8b27d7b69daff39fb62da7d40fe9834385986e924d4dae34313d8e03e29486d7acf392d2396d06ec16163b0bba7abd933bbb3e9
7
+ data.tar.gz: 7a66faa06156f420af5e8c31acdb3740b0a66266c738782dfded6337e923b88be119d242492b38c0ec5a5dc510c0e1061ab25139f2d5893613e6473d0130876c
@@ -0,0 +1,161 @@
1
+ require 'cgi'
2
+ require 'openssl'
3
+ require 'optparse'
4
+
5
+
6
+ ENV['TZ'] = 'GMT'
7
+
8
+ module Akamai
9
+ class AuthTokenError < Exception
10
+ """Base-class for all exceptions raised by AuthToken Class"""
11
+ end
12
+
13
+
14
+ class AuthToken
15
+ attr_accessor :token_type, :token_name, :key, :algorithm, :salt,
16
+ :start_time, :end_time, :window_secondse, :field_delimiter,
17
+ :acl_delimiter, :escape_early, :verbose
18
+
19
+ def initialize(token_type: nil, token_name: '__token__', key: nil,
20
+ algorithm: 'sha256', salt: nil, start_time: nil, end_time: nil,
21
+ window_seconds: nil, field_delimiter: '~', acl_delimiter: '!',
22
+ escape_early: false, verbose: false)
23
+
24
+ @token_type = token_type
25
+ @token_name = token_name
26
+ @start_time = start_time
27
+ @end_time = end_time
28
+ @window_seconds = window_seconds
29
+ if !key || key.length <= 0
30
+ raise AuthTokenError,
31
+ 'You must provide a secret in order to generate a new token.'
32
+ end
33
+ @key = key
34
+ @algorithm = algorithm
35
+ @salt = salt
36
+ @field_delimiter = field_delimiter
37
+ @acl_delimiter = acl_delimiter
38
+ @escape_early = escape_early
39
+ @verbose = verbose
40
+ end
41
+
42
+ def _escapeEarly(text)
43
+ if @escape_early
44
+ return CGI::escape(text).gsub(/(%..)/) {$1.downcase}
45
+ else
46
+ return text
47
+ end
48
+ end
49
+
50
+ def generateToken(url: nil, acl: nil, start_time: nil, end_time: nil, window_seconds: nil,
51
+ ip: nil, payload: nil, session_id: nil)
52
+
53
+ if !start_time
54
+ start_time = @start_time
55
+ end
56
+ if !end_time
57
+ end_time = @end_time
58
+ end
59
+ if !window_seconds
60
+ window_seconds = @window_seconds
61
+ end
62
+
63
+ if start_time.to_s.downcase == 'now'
64
+ start_time = Time.new.getgm.to_i
65
+ elsif start_time && !(start_time.is_a? Integer)
66
+ raise AuthTokenError, 'start_time must be numeric or now'
67
+ end
68
+
69
+ if end_time && !(end_time.is_a? Integer)
70
+ raise AuthTokenError, 'end_time must be numeric or now'
71
+ end
72
+
73
+ if window_seconds && !(window_seconds.is_a? Integer)
74
+ raise AuthTokenError, 'window_seconds must be numeric or now'
75
+ end
76
+
77
+ if !end_time
78
+ if window_seconds.to_i > 0
79
+ if !start_time
80
+ end_time = Time.new.getgm.to_i + window_seconds
81
+ else
82
+ end_time = start_time + window_seconds
83
+ end
84
+ else
85
+ raise AuthTokenError, 'You must provide an expiration time or a duration window..'
86
+ end
87
+ end
88
+
89
+ if start_time && end_time < start_time
90
+ raise AuthTokenError, 'Token will have already expired.'
91
+ end
92
+
93
+ if (!acl && !url) || (acl && url)
94
+ raise AuthTokenError, 'You must provide a URL or an ACL'
95
+ end
96
+
97
+ if @verbose
98
+ puts "Akamai Token Generation Parameters"
99
+ puts "Token Type : #{@token_type}"
100
+ puts "Token Name : #{@token_name}"
101
+ puts "Start Time : #{start_time}"
102
+ puts "End Time : #{end_time}"
103
+ puts "Window(seconds) : #{window_seconds}"
104
+ puts "IP : #{ip}"
105
+ puts "URL : #{url}"
106
+ puts "ACL : #{acl}"
107
+ puts "Key/Secret : #{@key}"
108
+ puts "Payload : #{payload}"
109
+ puts "Algo : #{@algo}"
110
+ puts "Salt : #{@salt}"
111
+ puts "Session ID : #{session_id}"
112
+ puts "Field Delimiter : #{@field_delimiter}"
113
+ puts "ACL Delimiter : #{@acl_delimiter}"
114
+ puts "Escape Early : #{@escape_early}"
115
+ end
116
+
117
+ hash_code = Array.new
118
+ new_token = Array.new
119
+
120
+ if ip
121
+ new_token.push('ip=%s' % _escapeEarly(ip))
122
+ end
123
+ if start_time
124
+ new_token.push('st=%s' % start_time)
125
+ end
126
+ new_token.push('exp=%s' % end_time)
127
+
128
+ if acl
129
+ new_token.push('acl=%s' % acl)
130
+ end
131
+ if session_id
132
+ new_token.push('id=%s' % _escapeEarly(session_id))
133
+ end
134
+ if payload
135
+ new_token.push('data=%s' % _escapeEarly(payload))
136
+ end
137
+
138
+ hash_code = new_token.clone
139
+
140
+ if url and !acl
141
+ hash_code.push('url=%s' % _escapeEarly(url))
142
+ end
143
+
144
+ if @salt
145
+ hash_code.push('salt=%s' % @salt)
146
+ end
147
+ if !(['sha256', 'sha1', 'md5'].include? @algorithm)
148
+ raise AuthTokenError, 'Unknown algorithm'
149
+ end
150
+
151
+ bin_key = Array(@key.gsub(/\s/,'')).pack("H*")
152
+ digest = OpenSSL::Digest.new(@algorithm)
153
+ token_hmac = OpenSSL::HMAC.new(bin_key, digest)
154
+ token_hmac.update(hash_code.join(@field_delimiter))
155
+
156
+ new_token.push('hmac=%s' % token_hmac)
157
+
158
+ return new_token.join(@field_delimiter)
159
+ end
160
+ end
161
+ end
metadata ADDED
@@ -0,0 +1,44 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: akamai-authtoken
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.4.0
5
+ platform: ruby
6
+ authors:
7
+ - Astin Choi
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-03-09 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Akamai-AuthToken is Akamai Authorization Token for Ruby 2.0+
14
+ email: achoi@akamai.com
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - lib/akamai/authtoken.rb
20
+ homepage: https://github.com/AstinCHOI/AkamaiAuthToken-Ruby
21
+ licenses:
22
+ - Apache
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.6.8
41
+ signing_key:
42
+ specification_version: 4
43
+ summary: Akamai Authorization Token for Ruby
44
+ test_files: []