Class: CS50

Inherits:
Object
  • Object
show all
Defined in:
lib/cs50.rb

Overview

User authentication using CS50 ID.

Licensed under the / Creative Commons Attribution-ShareAlike 3.0 Unported License

Class Method Summary collapse

Class Method Details

.getLoginUrl(directory, trust_root, return_to, session) ⇒ String

Get URL to which user can be redirected to authenticate using CS50 ID.

Parameters:

  • directory (String)

    Path to directory used to store state (i.e., Rails.root.join("tmp") for Ruby on Rails)

  • trust_root (String)

    URL that CS50 ID should prompt user to trust

  • return_to (String)

    URL to which CS50 should return user after login

  • session

    Session variable (i.e. session for Ruby on Rails)

Returns:

  • (String)

    URL for CS50 ID authentication



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/cs50.rb', line 25

def self.getLoginUrl(directory, trust_root, return_to, session)
    # prepare request
    store = OpenID::Store::Filesystem.new(Pathname.new(directory))
    consumer = OpenID::Consumer.new(session, store)
    auth_request = consumer.begin("https://id.cs50.net/")

    # simple registration fields
    auth_request.add_extension(OpenID::SReg::Request.new(["email"], ["fullname"]))

=begin AX code, removed for now due to URL length limitation in WEBrick
    # attribute exchange attributes
    ax_request = OpenID::AX::FetchRequest.new
    ax_request.add(OpenID::AX::AttrInfo.new("http://axschema.org/contact/email", 1, true))
    ax_request.add(OpenID::AX::AttrInfo.new("http://axschema.org/contact/namePerson", 2, false))
    auth_request.add_extension(ax_request)
=end

    # generate url for redirection
    return auth_request.redirect_url(trust_root, return_to)
end

.getUser(directory, return_to, session, params) ⇒ Hash

Note:

A unique ID for the user will be returned, and the user’s email and name may be returned.

If user has been authenticated by CS50 ID, get the user’s information.

Parameters:

  • directory (String)

    Path to directory used to store state (i.e., Rails.root.join("tmp") for Ruby on Rails)

  • return_to (String)

    URL to which CS50 should return user after login

  • session

    Session variable (i.e., session for Ruby on Rails)

  • params

    Parameters array (i.e., params for Ruby on Rails)

Returns:

  • (Hash)

    User’s :id, :email and :name



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/cs50.rb', line 55

def self.getUser(directory, return_to, session, params)
    # clean rails parameters from the URL (else Janrain fails)
    parameters = params.clone
    parameters.delete(:controller)
    parameters.delete(:action)

    # get response
    store = OpenID::Store::Filesystem.new(Pathname.new(directory))
    consumer = OpenID::Consumer.new(session, store)
    response = consumer.complete(parameters, return_to)

    if (response.status == OpenID::Consumer::SUCCESS)
        # simple registration fields
        sreg_resp = OpenID::SReg::Response.from_success_response(response)
        
=begin AX code, removed for now due to URL length limitation in WEBrick
        # get attribute exchange attributes
        ax_resp = OpenID::AX::FetchResponse.from_success_response(response)
        data = ax_resp.data
=end

        # get user's ID from identity url
        if (response.identity_url =~ /^https:\/\/id.cs50.net\/([0123456789abcdef]{64})$/)
            user = { :id => $1 }
        else
            return false
        end

        # get user's email
        if (sreg_resp["email"])
            user[:email] = sreg_resp["email"]
        end
            
        # get user's name
        if (sreg_resp["fullname"])
            user[:name] = sreg_resp["fullname"]
        end

        return user
        
    # response failure
    else
        return false
    end
end