cs50 0.1.2 → 0.1.3

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 (2) hide show
  1. data/lib/cs50.rb +21 -28
  2. metadata +3 -3
@@ -20,24 +20,29 @@ class CS50
20
20
  # @param [String] trust_root URL that CS50 ID should prompt user to trust
21
21
  # @param [String] return_to URL to which CS50 should return user after login
22
22
  # @param session Session variable (i.e. <tt>session</tt> for Ruby on Rails)
23
+ # @param [Array] fields Simple registration fields
24
+ # @param [Array] attributes Attribute exchange fields
23
25
  # @return [String] URL for CS50 ID authentication
24
26
  #
25
- def self.getLoginUrl(directory, trust_root, return_to, session)
27
+ def self.getLoginUrl(directory, trust_root, return_to, session, fields = ["email", "fullname"], attributes = [])
26
28
  # prepare request
27
29
  store = OpenID::Store::Filesystem.new(Pathname.new(directory))
28
30
  consumer = OpenID::Consumer.new(session, store)
29
31
  auth_request = consumer.begin("https://id.cs50.net/")
30
32
 
31
33
  # simple registration fields
32
- auth_request.add_extension(OpenID::SReg::Request.new(["email"], ["fullname"]))
34
+ if (fields.kind_of?(Array) && fields.length > 0)
35
+ auth_request.add_extension(OpenID::SReg::Request.new(nil, fields))
36
+ end
33
37
 
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
38
+ # attribute exchange fields
39
+ if (attributes.kind_of?(Array) && attributes.length > 0)
40
+ ax_request = OpenID::AX::FetchRequest.new
41
+ attributes.each do |attribute|
42
+ ax_request.add(OpenID::AX::AttrInfo.new(attribute, 1, false))
43
+ end
44
+ auth_request.add_extension(ax_request)
45
+ end
41
46
 
42
47
  # generate url for redirection
43
48
  return auth_request.redirect_url(trust_root, return_to)
@@ -64,30 +69,18 @@ class CS50
64
69
  response = consumer.complete(parameters, return_to)
65
70
 
66
71
  if (response.status == OpenID::Consumer::SUCCESS)
72
+ user = { "identity" => response.identity_url }
73
+
67
74
  # simple registration fields
68
75
  sreg_resp = OpenID::SReg::Response.from_success_response(response)
76
+ if (sreg_resp)
77
+ user.merge!(sreg_resp.data)
78
+ end
69
79
 
70
- =begin AX code, removed for now due to URL length limitation in WEBrick
71
80
  # get attribute exchange attributes
72
81
  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"]
82
+ if (ax_resp)
83
+ user.merge!(ax_resp.data)
91
84
  end
92
85
 
93
86
  return user
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: cs50
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.1.2
5
+ version: 0.1.3
6
6
  platform: ruby
7
7
  authors:
8
8
  - Tommy MacWilliam
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2011-06-13 00:00:00 -07:00
13
+ date: 2011-07-08 00:00:00 -07:00
14
14
  default_executable:
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
@@ -24,7 +24,7 @@ dependencies:
24
24
  version: "0"
25
25
  type: :runtime
26
26
  version_requirements: *id001
27
- description: Utility functions for authenticating users via CS50 ID (https://id.cs50.net/)
27
+ description: Utility functions for authenticating users via Harvard's OpenID provider, CS50 ID (https://id.cs50.net/)
28
28
  email: macwilliamt@gmail.com
29
29
  executables: []
30
30