Description:
fix cucas authen, the old code work on 2.1.2 but not 1.9.2
Commit status:
[Not Reviewed]
References:
Comments:
0 Commit comments 0 Inline Comments
Unresolved TODOs:
There are no unresolved TODOs
Add another comment

r407:b11b67ad58fb - - 1 file changed: 9 inserted, 2 deleted

@@ -64,98 +64,105
64 64 @@per_page = 50
65 65
66 66 def self.authenticate(login, password)
67 67 user = find_by_login(login)
68 68 if user
69 69 return user if user.authenticated?(password)
70 70 if user.authenticated_by_cucas?(password) or user.authenticated_by_pop3?(password)
71 71 user.password = password
72 72 user.save
73 73 return user
74 74 end
75 75 end
76 76 end
77 77
78 78 def authenticated?(password)
79 79 if self.activated
80 80 hashed_password == User.encrypt(password,self.salt)
81 81 else
82 82 false
83 83 end
84 84 end
85 85
86 86 def authenticated_by_pop3?(password)
87 87 Net::POP3.enable_ssl
88 88 pop = Net::POP3.new('pops.it.chula.ac.th')
89 89 authen = true
90 90 begin
91 91 pop.start(login, password)
92 92 pop.finish
93 93 return true
94 94 rescue
95 95 return false
96 96 end
97 97 end
98 98
99 99 def authenticated_by_cucas?(password)
100 100 url = URI.parse('https://www.cas.chula.ac.th/cas/api/?q=studentAuthenticate')
101 101 appid = '41508763e340d5858c00f8c1a0f5a2bb'
102 102 appsecret ='d9cbb5863091dbe186fded85722a1e31'
103 103 post_args = {
104 104 'appid' => appid,
105 105 'appsecret' => appsecret,
106 106 'username' => login,
107 107 'password' => password
108 108 }
109 109
110 110 #simple call
111 111 begin
112 - resp = Net::HTTP.post_form(url, post_args)
113 - result = JSON.parse resp.body
112 + http = Net::HTTP.new('www.cas.chula.ac.th', 443)
113 + http.use_ssl = true
114 + result = [ ]
115 + http.start do |http|
116 + req = Net::HTTP::Post.new('/cas/api/?q=studentAuthenticate')
117 + param = "appid=#{appid}&appsecret=#{appsecret}&username=#{login}&password=#{password}"
118 + resp = http.request(req,param)
119 + result = JSON.parse resp.body
120 + end
114 121 return true if result["type"] == "beanStudent"
115 122 rescue
116 123 return false
117 124 end
118 125 return false
119 126 end
120 127
121 128 def admin?
122 129 self.roles.detect {|r| r.name == 'admin' }
123 130 end
124 131
125 132 def email_for_editing
126 133 if self.email==nil
127 134 "(unknown)"
128 135 elsif self.email==''
129 136 "(blank)"
130 137 else
131 138 self.email
132 139 end
133 140 end
134 141
135 142 def email_for_editing=(e)
136 143 self.email=e
137 144 end
138 145
139 146 def alias_for_editing
140 147 if self.alias==nil
141 148 "(unknown)"
142 149 elsif self.alias==''
143 150 "(blank)"
144 151 else
145 152 self.alias
146 153 end
147 154 end
148 155
149 156 def alias_for_editing=(e)
150 157 self.alias=e
151 158 end
152 159
153 160 def activation_key
154 161 if self.hashed_password==nil
155 162 encrypt_new_password
156 163 end
157 164 Digest::SHA1.hexdigest(self.hashed_password)[0..7]
158 165 end
159 166
160 167 def verify_activation_key(key)
161 168 key == activation_key
You need to be logged in to leave comments. Login now