CAS Authenticatable Integration with Devise

Marco Maigua
The Blockchain Artist
3 min readJan 16, 2020

It has been a while since I have written. Lots of things happened. I got another job. Even though I am still reading and training myself about blockchain technology, I got this great opportunity to assist a great private company as a DevOps developer. The switch of tasks is really challenging because know I dont have to focus on a specific framework but overall I have to worry a lot about security, integration of authentication systems, and then individual projects.

Anyway, it happened that my current employer needed the integration of an open-source project for slides that needed to integrate(SLIDEHUB), THEIR CAS server which gives the employees information to login and DEVISE AUTHENTICATION.

There was a small problem, the project was developed in Ruby on Rails. Although I have had training in Laravel PHP and a little bit of DJango, I was completely unfamiliar with Ruby on Rails. The learning curve was difficult. I spent weeks in tutorials trying to find a way to understand the complex code. Once I completed the customization, the next big challenge was to INCORPORATE the CAS server of my company with this application.

Immediately I looked for options, the main option that came to mind was to use cas_authenticatable which is a submodule of devise. The module seemed to be very serious so I followed the main guidelines in the ReadMe file:

  • install the gem
gem 'devise' # in the Gemfile
bundle install
rails generate devise:install
  • aggregate devise :cas_authenticatable in the user model. Additionally, the user needs the extra attributes to map the user devise user model when using the helper methods ‘current_user’
  • set new params in the database user table.
  • add the url of the redirection of cas

# config.cas_login_url = "https://cas.myorganization.com/login" in the devise.rb

After every step was implemented, everything seemed to be working. The SignIn Button of the application redirected me to our CAS server login. Thereafter, the response was printed in the user model in the extra attributes method.

Although, the main problem was that once the credentials were inserted, the redirection to the application with a session open for the user was not working. The application was still blocked as if this were still not logged in.

Therefore, after several attempts, I found a solution that I couldn't find anywhere else. To manually sign_in the user in the application controller.

It didn't seem a very difficult approach but at the time I was completely lost in my Ruby on Rails knowledge.

Therefore, the following ApplicationController has a method that evaluates the existence of a session. In my case, I caught the ‘current_user’ helper method to get the data from the CAS server and then I signed the user manually by code. This method was specific for my application but it will depend on your application. Finally, the method ‘current_user’ and ‘sign_in’ should be available in any controller.

ApplicationController.rb

Any question or concern I am happy to help because I was really stuck in this problem and I feel someone else could have the same problem.

If you want to know more about me and my projects follow me in my social media:

Website, LinkedIn, Gihub

--

--