Configuring ActiveResource to support OAuth2
Asked Answered
C

1

6

I need to be able configure ActiveResource to connect with OAuth2 or basic authentication on a connection by connection basis. I have found a couple ways to configure ActiveResource with OAuth2, but they don't seem that elegant and don't lend themselves to a dynamic type configuration. Any help out there?

Citral answered 11/3, 2012 at 15:36 Comment(2)
Have you taken a look at Koala? We use it for dynamic Oauth authentication.Alexandros
Please explain specificially what you mean by "dynamic type configuration".Chasten
C
5

I figured out how to do this by having my ActiveResource classes inherit from an intermediate class:

  class Resource < ActiveResource::Base
  end

  class MyClass < Resource
  end

This allows you to dynamically set the authentication (as well as site, format, etc) for all classes that inherit from the intermediate Resource class:

if the user has OAuth2 configured:

   Resource.headers['authorization'] = 'Bearer ' + my_oauth2_token

or if the user is just using basic authentication:

   Resource.user = my_user_name
   Resource.password = my_password

Hope this helps someone!

Citral answered 6/4, 2012 at 13:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.