{"id":164,"date":"2014-01-26T16:46:23","date_gmt":"2014-01-26T21:46:23","guid":{"rendered":"http:\/\/qxf2.com\/blog\/?p=164"},"modified":"2015-04-12T08:38:24","modified_gmt":"2015-04-12T12:38:24","slug":"cas-automated-login","status":"publish","type":"post","link":"https:\/\/qxf2.com\/blog\/cas-automated-login\/","title":{"rendered":"CAS automated login"},"content":{"rendered":"<p>Does your application uses <a title=\"CAS\" href=\"http:\/\/www.jasig.org\/cas\">CAS<\/a> for authentication? Unless your CAS server has already been configured to allow for scripted login, automating login for applications that use CAS could be tough.<\/p>\n<p>One way we automated the login operation for an application that uses CAS for authentication was to mimic the browser. Python is the language of choice at <a href=\"http:\/\/www.qxf2.com\" title=\"Qxf2 Services\">Qxf2 Services<\/a>. To mimic the browser we chose the <a title=\"Mechanize\" href=\"http:\/\/wwwsearch.sourceforge.net\/mechanize\/\">Mechanize<\/a> module. <\/p>\n<hr>\n<h1>Install Mechanize<\/h1>\n<p><a title=\"Python Package Index\" href=\"https:\/\/pypi.python.org\/pypi\/pip\">Python Package Index<\/a> aka pip is the easiest way to install most modules in Python. If you are on a Windows machine, you can get pip by following the instructions <a title=\"here\" href=\"http:\/\/docs.python-guide.org\/en\/latest\/starting\/install\/win\/\">here<\/a>. Once you have pip installed and in your path, you can install Mechanize by opening a command prompt and running the command:<\/p>\n<pre lang=\"python\">pip install -U Mechanize<\/pre>\n<hr>\n<h1>Code snippet<\/h1>\n<p>Here is the actual code snippet for you to login to an application that sits behind CAS: <\/p>\n<hr>\n<pre lang=\"python\">import mechanize\r\nurl = \"YOUR_URL\"\r\nbr = mechanize.Browser()\r\nbr.set_handle_robots(False) #You may need to do this\r\nbr.open(url)\r\nbr.select_form(nr=0) #Choose the right form number. You can choose the form via the name attribute too select_form(name=\"YOUR_FORM_NAME\")\r\nbr['username']=\"USERNAME\"\r\nbr['password']=\"FILL_PASSWORD_HERE\"\r\nbr.method = \"POST\"\r\nresponse = br.submit() #At this point you should see the html for the page that loads after login\r\nprint response.read()\r\n<\/pre>\n<hr>\n<p><strong>WARNING:<\/strong> Mechanize has its limitations as an automated testing tool. The biggest drawback for web testers is that Mechanize does not mimic rendering of executed JavaScript in the browser. So DOM elements that are rendered by executing JavaScript cannot be accessed by Mechanize.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Does your application uses CAS for authentication? Unless your CAS server has already been configured to allow for scripted login, automating login for applications that use CAS could be tough. One way we automated the login operation for an application that uses CAS for authentication was to mimic the browser. Python is the language of choice at Qxf2 Services. To [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[15,18],"tags":[23,25,24],"class_list":["post-164","post","type-post","status-publish","format-standard","hentry","category-how-to","category-python","tag-cas","tag-mechanize","tag-python-2"],"_links":{"self":[{"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/posts\/164","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/comments?post=164"}],"version-history":[{"count":27,"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/posts\/164\/revisions"}],"predecessor-version":[{"id":266,"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/posts\/164\/revisions\/266"}],"wp:attachment":[{"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/media?parent=164"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/categories?post=164"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/tags?post=164"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}