cs50 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/cs50.rb +100 -0
  2. metadata +66 -0
@@ -0,0 +1,100 @@
1
+ require 'openid'
2
+ require 'openid/extensions/ax'
3
+ require 'openid/extensions/sreg'
4
+ require 'openid/store/filesystem'
5
+ require 'pathname'
6
+
7
+ # CS50 OpenID authentication
8
+ #
9
+ # Licensed under the {http://creativecommons.org/licenses/by-sa/3.0/ Creative Commons Attribution-ShareAlike 3.0 Unported License}
10
+ #
11
+ # @author {mailto:macwilliamt@gmail.com Tommy MacWilliam}
12
+ # @version 0.1
13
+ # @see https://manual.cs50.net/CS50_Library
14
+ #
15
+ class CS50
16
+
17
+ # Get URL to which user can be redirected to authenticate using CS50 ID.
18
+ #
19
+ # @param [String] directory Path to directory used to store state (i.e. <tt>Rails.root.join('tmp')</tt> for Ruby on Rails)
20
+ # @param [String] trust_root URL that CS50 ID should prompt user to trust
21
+ # @param [String] return_to URL to which CS50 should return user after login
22
+ # @param session Session variable (i.e. <tt>session</tt> for Ruby on Rails)
23
+ # @return [String] URL for CS50 ID authentication
24
+ #
25
+ def self.getLoginUrl(directory, trust_root, return_to, session)
26
+ # prepare request
27
+ store = OpenID::Store::Filesystem.new(Pathname.new(directory))
28
+ consumer = OpenID::Consumer.new(session, store)
29
+ auth_request = consumer.begin('https://id.cs50.net/')
30
+
31
+ # simple registration fields
32
+ auth_request.add_extension(OpenID::SReg::Request.new(['email'], ['fullname']))
33
+
34
+ =begin AX code, removed for now due to URL length limitation in WEBrick
35
+ # attribute exchange attributes
36
+ ax_request = OpenID::AX::FetchRequest.new
37
+ ax_request.add(OpenID::AX::AttrInfo.new("http://axschema.org/contact/email", 1, true))
38
+ ax_request.add(OpenID::AX::AttrInfo.new("http://axschema.org/contact/namePerson", 2, false))
39
+ auth_request.add_extension(ax_request)
40
+ =end
41
+
42
+ # generate url for redirection
43
+ return auth_request.redirect_url(trust_root, return_to)
44
+ end
45
+
46
+ # If user has been authenticated by CS50 ID, get the user's information.
47
+ # @note A unique ID for the user *will* be returned, and the user's email and name *may* be returned.
48
+ #
49
+ # @param [String] directory Path to directory used to store state (i.e. <tt>Rails.root.join('tmp')</tt> for Ruby on Rails)
50
+ # @param [String] return_to URL to which CS50 should return user after login
51
+ # @param session Session variable (i.e. <tt>session</tt> for Ruby on Rails)
52
+ # @param params Parameters array (i.e. <tt>params</tt> for Ruby on Rails)
53
+ # @return [Hash] User's <tt>:id</tt>, <tt>:email</tt> and <tt>:name</tt>
54
+ #
55
+ def self.getUser(directory, return_to, session, params)
56
+ # clean rails parameters from the URL (else Janrain fails)
57
+ parameters = params.clone
58
+ parameters.delete(:controller)
59
+ parameters.delete(:action)
60
+
61
+ # get response
62
+ store = OpenID::Store::Filesystem.new(Pathname.new(directory))
63
+ consumer = OpenID::Consumer.new(session, store)
64
+ response = consumer.complete(parameters, return_to)
65
+
66
+ if (response.status == OpenID::Consumer::SUCCESS)
67
+ # simple registration fields
68
+ sreg_resp = OpenID::SReg::Response.from_success_response(response)
69
+
70
+ =begin AX code, removed for now due to URL length limitation in WEBrick
71
+ # get attribute exchange attributes
72
+ ax_resp = OpenID::AX::FetchResponse.from_success_response(response)
73
+ data = ax_resp.data
74
+ =end
75
+
76
+ # get user's ID from identity url
77
+ if (response.identity_url =~ /^https:\/\/id.cs50.net\/([0123456789abcdef]{64})$/)
78
+ user = { :id => $1 }
79
+ else
80
+ return false
81
+ end
82
+
83
+ # get user's email
84
+ if (sreg_resp["email"])
85
+ user[:email] = sreg_resp["email"]
86
+ end
87
+
88
+ # get user's name
89
+ if (sreg_resp["fullname"])
90
+ user[:name] = sreg_resp["fullname"]
91
+ end
92
+
93
+ return user;
94
+
95
+ # response failure
96
+ else
97
+ return false
98
+ end
99
+ end
100
+ end
metadata ADDED
@@ -0,0 +1,66 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cs50
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.1.1
6
+ platform: ruby
7
+ authors:
8
+ - Tommy MacWilliam
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2011-06-12 00:00:00 -07:00
14
+ default_executable:
15
+ dependencies:
16
+ - !ruby/object:Gem::Dependency
17
+ name: ruby-openid
18
+ prerelease: false
19
+ requirement: &id001 !ruby/object:Gem::Requirement
20
+ none: false
21
+ requirements:
22
+ - - ">="
23
+ - !ruby/object:Gem::Version
24
+ version: "0"
25
+ type: :runtime
26
+ version_requirements: *id001
27
+ description: Utility functions for authenticating users via CS50 ID (https://id.cs50.net/)
28
+ email: macwilliamt@gmail.com
29
+ executables: []
30
+
31
+ extensions: []
32
+
33
+ extra_rdoc_files: []
34
+
35
+ files:
36
+ - lib/cs50.rb
37
+ has_rdoc: true
38
+ homepage: https://docs.cs50.net/CS50_ID
39
+ licenses: []
40
+
41
+ post_install_message:
42
+ rdoc_options: []
43
+
44
+ require_paths:
45
+ - lib
46
+ required_ruby_version: !ruby/object:Gem::Requirement
47
+ none: false
48
+ requirements:
49
+ - - ">="
50
+ - !ruby/object:Gem::Version
51
+ version: "0"
52
+ required_rubygems_version: !ruby/object:Gem::Requirement
53
+ none: false
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ version: "0"
58
+ requirements: []
59
+
60
+ rubyforge_project:
61
+ rubygems_version: 1.6.2
62
+ signing_key:
63
+ specification_version: 3
64
+ summary: Library for Harvard College's CS50
65
+ test_files: []
66
+