3

I've built an intranet application on Ruby on Rails running on Windows. Everyone who will be accessing this app is running on Windows and is already logged into the network with Windows.

All I need to do is to grab the login name of the logged in user and save that login name to a session variable. I tried ENV['USERNAME'] but that only shows the person who is logged into the server box, which is me.

Is there any way to automatically grab and store that variable from the users who are access the site? I know that I could ask them for it but I assume that there must be some way to grab it.

Thanks...Chris

thinkfuture
  • 241
  • 1
  • 5
  • 12
  • It's not something I've actually done, but what you're looking for is "NTLM authentication". Plugging that into google might give you some ideas. e.g. http://blog.rayapps.com/2008/12/02/ntlm-windows-domain-authentication-for-rails-application/ is the first result for "rails ntlm authentication". – matt Mar 31 '11 at 23:56
  • possible duplicate of [Ruby: Get currently logged in user on windows](http://stackoverflow.com/questions/3251757/ruby-get-currently-logged-in-user-on-windows) – Peter O. Jun 15 '15 at 23:38

3 Answers3

1

No, you will need to setup an authentication system within your rails application. Accessing the clients environment would be a huge security breach in a number of systems such as the browser. Depending on your requirements there are a number of nice plugins for rails to handle all of this authentication for you. I personally like devise. There are a few plugins for it and one that would authenticate with an LDAP server if you did not wish to store username/passwords and make users maintain a separate set of login credentials for your intranet application. This, however, is very common.

jeremy
  • 130
  • 1
  • 7
  • So there is no way to bypass a login? I have to ask for one? How does single sign on work on all these systems that are out there then? – thinkfuture Mar 31 '11 at 23:21
1

This site has a purported answer, but it's IE-only. And since browser's really aren't supposed to make that sort of information available, this may have been removed from newer versions of IE. The post's from late 2008, so...

As much as I hate IE and platform-dependentness, if you're stuck on a Windows-only, IE-only corporate intranet anyway, this might be the way to go.

Hope this helps!

PS: If people will just be accessing your app through their work machines, what about using MAC / local (desktop) IP addresses? MAC would be more reliable, of course, but it's also IE only...

Xavier Holt
  • 14,471
  • 4
  • 43
  • 56
0

Same question here: Rails get username of currently logged windows user

Anyways, just copying my own answer below...


This is what worked for me but there are some limitations:

If you don't care about these issues, go ahead:

  1. In your rails application, add Rekado's gem to your Gemfile: gem 'ntlm-sso', '=0.0.1'

  2. Create an initialiser config/initializers/ntlm-sso.rb with:

    require 'rack'
    require 'rack/auth/ntlm-sso'
    
    class NTLMAuthentication
      def initialize(app)
        @app = app
      end
    
      def call(env)
        auth = Rack::Auth::NTLMSSO.new(@app)
        return auth.call(env)
      end
    end
    
  3. On your application.rb file, add the line: config.middleware.use "NTLMAuthentication"

  4. Call request.env["REMOTE_USER"] on your view or controller to get current username.

PS: Let me know if you find anyway to make it work on Chrome or to validate user credentials.

Community
  • 1
  • 1
Flavio Wuensche
  • 9,460
  • 1
  • 57
  • 54