mod-auth-pubtkt-rb 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/README.md +49 -0
  2. data/lib/mod_auth_pubtkt.rb +110 -0
  3. metadata +65 -0
@@ -0,0 +1,49 @@
1
+ # mod\_auth_pubtkt.rb
2
+
3
+ Here is a simple module for generating correctly signed tickets for use with the mod\_auth_pubtkt Apache module, pretty basic stuff but usefully abstracts the OpenSSL complications.
4
+
5
+ For more info on mod\_auth\_pubtkt see: [https://neon1.net/mod\_auth_pubtkt](https://neon1.net/mod\_auth_pubtkt/)
6
+
7
+ ## Install
8
+
9
+ sudo gem install mod-auth-pubtkt-rb
10
+
11
+ ## Usage
12
+
13
+ ### Generate a public / private key pair
14
+
15
+ Taken from: [https://neon1.net/mod\_auth_pubtkt/install.html](https://neon1.net/mod\_auth_pubtkt/install.html)
16
+
17
+ #### DSA
18
+
19
+ # openssl dsaparam -out dsaparam.pem 1024
20
+ # openssl gendsa -out privkey.pem dsaparam.pem
21
+ # openssl dsa -in privkey.pem -out pubkey.pem -pubout
22
+
23
+ The dsaparam.pem file is not needed anymore after key generation and can safely be deleted.
24
+
25
+ #### RSA
26
+
27
+ # openssl genrsa -out privkey.pem 1024
28
+ # openssl rsa -in privkey.pem -out pubkey.pem -pubout
29
+
30
+ ### Use it in your code
31
+
32
+ require 'mod_auth_pubtkt'
33
+
34
+ # This will generate the ticket, see ./lib/mod_auth_pubtkt for available options
35
+ tkt = ModAuthPubTkt.create_ticket 12345, Time.now + 3600, "/my/privkey.pem", "DSA"
36
+
37
+ # Now you can use the ticket as a cookie value in your web app!
38
+
39
+ ## License
40
+
41
+ (GPLv3)
42
+
43
+ Copyright (C) 2010 Matt Haynes
44
+
45
+ This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
46
+
47
+ This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
48
+
49
+ You should have received a copy of the GNU General Public License along with this program. If not, see <www.gnu.org/licenses/>
@@ -0,0 +1,110 @@
1
+ require 'openssl'
2
+ require 'base64'
3
+
4
+ #
5
+ # A ruby module for creating tickets that are compatible with the Apache module
6
+ # mod_auth_pubtkt.
7
+ #
8
+ # See https://neon1.net/mod_auth_pubtkt/ for more details
9
+ #
10
+ # @author Matt Haynes <matt@matthaynes.net>
11
+ #
12
+ module ModAuthPubTkt
13
+
14
+ #
15
+ # Create a ticket for use in a mod_auth_pubtkt cookie
16
+ #
17
+ # See https://neon1.net/mod_auth_pubtkt/ for more details
18
+ #
19
+ # === Parameters
20
+ #
21
+ # - uid: (required; 32 chars max.)
22
+ # The user ID / username the ticket has been issued for, passed to the environment in REMOTE_USER
23
+ #
24
+ # - expires: (required.)
25
+ # A Time object that describes when this ticket will expire
26
+ #
27
+ # - key_path: (required.)
28
+ # Path to your SSL key to sign the ticket with
29
+ #
30
+ # - key_type: (required.)
31
+ # The type of key ("RSA" or "DSA")
32
+ #
33
+ # - cip: (optional; 39 chars max.)
34
+ # The client IP address.
35
+ #
36
+ # - tokens: (optional; 255 chars max.)
37
+ # A comma-separated list of words (group names etc.) The contents of this field are available
38
+ # to the environment in REMOTE_USER_TOKENS
39
+ #
40
+ # - udata: (optional; 255 chars max.)
41
+ # User data, for use by scripts; made available to the environment in REMOTE_USER_DATA
42
+ #
43
+ # - grace_period: (optional)
44
+ # A number of seconds grace period before ticket is refreshed
45
+ #
46
+ def create_ticket(uid, expires, key_path, key_type, cip = '', tokens = '', udata = '', grace_period = 0)
47
+
48
+ key = open_key_file(key_path, key_type)
49
+
50
+ tkt = "uid=#{uid};validuntil=#{expires.to_i};cip=#{cip};tokens=#{tokens};udata=#{udata};grace_period=17987";
51
+
52
+ sig = encrypt tkt, key
53
+
54
+ tkt + ";sig=" + Base64.b64encode(sig).gsub("\n", '').strip
55
+
56
+ end
57
+
58
+ # Verify a ticket is good / not been tampered with.
59
+ # NB: This should be done by the apache module but is useful for testing here too
60
+ def verify(tkt, key)
61
+
62
+ if tkt =~ /(.*);sig=(.*)/
63
+ str = $1
64
+ sig = Base64.decode64($2)
65
+ else
66
+ raise "Invalid ticket format"
67
+ end
68
+
69
+ if key.class == OpenSSL::PKey::DSA
70
+ key.verify(OpenSSL::Digest::DSS1.new, sig, str)
71
+ elsif key.class == OpenSSL::PKey::RSA
72
+ key.verify(OpenSSL::Digest::SHA1.new, sig, str)
73
+ end
74
+
75
+ end
76
+
77
+ # Encrypt the string using key
78
+ def encrypt(string, key)
79
+
80
+ if key.class == OpenSSL::PKey::DSA
81
+ key.sign(OpenSSL::Digest::DSS1.new, string)
82
+ elsif key.class == OpenSSL::PKey::RSA
83
+ key.sign(OpenSSL::Digest::SHA1.new, string)
84
+ end
85
+
86
+ end
87
+
88
+ # Get the SSL key
89
+ def open_key_file(path, type)
90
+ if type == 'DSA'
91
+ OpenSSL::PKey::DSA.new File.read(path)
92
+ elsif type == 'RSA'
93
+ OpenSSL::PKey::RSA.new File.read(path)
94
+ end
95
+ end
96
+
97
+
98
+ # function adapted according to php: generates an IPv4 Internet network address
99
+ # from its Internet standard format (dotted string) representation.
100
+ def ip2long(ip)
101
+ long = 0
102
+ ip.split( /\./ ).reverse.each_with_index do |x, i|
103
+ long += x.to_i << ( i * 8 )
104
+ end
105
+ long
106
+ end
107
+
108
+ module_function :create_ticket, :encrypt, :verify, :open_key_file, :ip2long
109
+
110
+ end
metadata ADDED
@@ -0,0 +1,65 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mod-auth-pubtkt-rb
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 0
8
+ - 1
9
+ version: 0.0.1
10
+ platform: ruby
11
+ authors:
12
+ - Matt Haynes
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-08-03 00:00:00 +01:00
18
+ default_executable:
19
+ dependencies: []
20
+
21
+ description: Here is a simple module for generating correctly signed tickets for use with the mod_auth_pubtkt Apache module, pretty basic stuff but usefully abstracts the OpenSSL complications.
22
+ email: matt@matthaynes.net
23
+ executables: []
24
+
25
+ extensions: []
26
+
27
+ extra_rdoc_files: []
28
+
29
+ files:
30
+ - lib/mod_auth_pubtkt.rb
31
+ - README.md
32
+ has_rdoc: false
33
+ homepage: http://github.com/matth/mod_auth_pubtkt_rb
34
+ licenses: []
35
+
36
+ post_install_message:
37
+ rdoc_options: []
38
+
39
+ require_paths:
40
+ - lib
41
+ required_ruby_version: !ruby/object:Gem::Requirement
42
+ none: false
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ segments:
47
+ - 0
48
+ version: "0"
49
+ required_rubygems_version: !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ segments:
55
+ - 0
56
+ version: "0"
57
+ requirements: []
58
+
59
+ rubyforge_project:
60
+ rubygems_version: 1.3.7
61
+ signing_key:
62
+ specification_version: 3
63
+ summary: A ruby library for creating tickets for the Apache mod_auth_pubtkt module
64
+ test_files: []
65
+