require 'omniauth' require 'omniauth-oauth2' require 'faraday_middleware' module Centro class Client class << self def api_version return @api_version if defined?(@api_version) @api_version = 'v1' end def auth_host return @auth_host if defined?(@auth_host) @auth_host = 'localhost:3001' end def mms_host return @mms_host if defined?(@mms_host) @mms_host = 'localhost:3000' end def ssl_enabled? return @ssl_enabled if defined?(@ssl_enabled) @ssl_enaabled = false end def auth_url url_from_host(auth_host) end def mms_url url_from_host(mms_host) end def url_from_host(host) (ssl_enabled? ? 'https://' : 'http://') + host end end attr_accessor :client_id, :client_secret, :access_token def initialize(opts={}) @client_id = opts.delete(:client_id) if opts[:client_id] @client_secret = opts.delete(:client_secret) if opts[:client_secret] @access_token = opts.delete(:access_token) if opts[:access_token] end def retrieve_access_token(username, password) raise 'client_id and client_secret required' unless @client_id && @client_secret connection = Faraday.new self.class.auth_url do |conn| set_default_connection_options(conn) end response = connection.post('/oauth/token', :grant_type => 'password', :client_id => @client_id, :client_secret => @client_secret, :username => username, :password => password) raise 'Failed to get an access token' unless response.success? && response.body['access_token'] self.access_token = response.body['access_token'] end def get_credentials auth_connection.get("/api/#{self.class.api_version}/me.json").body end def get_organizations(user_id=nil) options = {} options[:user_id]=user_id if user_id auth_connection.get("/api/#{self.class.api_version}/organizations.json", options).body end def get_members_of_organization(organization_id) auth_connection.get("/api/#{self.class.api_version}/organizations/#{organization_id}/members.json").body end ## # Creates a user in an organization. If the user already exists in AuthTransis they will # simply be added to the org (assuming the resource owner has rights), if they don't exist # they will be created and added to the organizations. # # Returns a list of the organizations that they were sucessfully added to. def create_user_in_organization(email, organization_id) org_ids = organization_id.respond_to?(:each) ? organization_id : [organization_id] successfuls = [] org_ids.each do |org_id| response = auth_connection.post("/api/#{self.class.api_version}/organizations/#{organization_id}/members.json", {:email_address => email}) response.success? && successfuls << response.body end successfuls end def create_user(email) response = auth_connection.post("/api/#{self.class.api_version}/members.json", {:email_address => email}) response.success? && response.body end def create_organization(org_name) response = auth_connection.post("/api/#{self.class.api_version}/organizations", {:name => org_name}) response.success? && response.body end private def set_default_connection_options(conn) conn.request :json conn.response :dates conn.response :mashify conn.response :json, :content_type => /\bjson$/ conn.response :raise_error conn.adapter Faraday.default_adapter end def auth_connection raise 'access_token required' unless @access_token @auth_connection ||= begin Faraday.new self.class.auth_url do |conn| conn.request :oauth2, @access_token set_default_connection_options(conn) end end end def mms_connection raise 'access_token required' unless @access_token @mms_connection ||= begin Faraday.new self.class.mms_url do |conn| conn.request :oauth2, @access_token set_default_connection_options(conn) end end end end end