Coder Social home page Coder Social logo

Generic Authentication about grape HOT 11 CLOSED

ruby-grape avatar ruby-grape commented on May 29, 2024
Generic Authentication

from grape.

Comments (11)

mbleigh avatar mbleigh commented on May 29, 2024

Should also be able to use authenticated and unauthenticated as a block to require authentication, e.g.

class MyAPI < Grape::API
  auth { something }

  authenticated do
    get '/me' do
      current_user
    end
  end
end

from grape.

rantav avatar rantav commented on May 29, 2024

+1

from grape.

DeTeam avatar DeTeam commented on May 29, 2024

Is anybody working on this now?

from grape.

ivanvanderbyl avatar ivanvanderbyl commented on May 29, 2024

+1 this is exactly what I need.

from grape.

keybits avatar keybits commented on May 29, 2024

+1 this would be very helpful.

from grape.

marcusg avatar marcusg commented on May 29, 2024

+1

from grape.

chrisbloom7 avatar chrisbloom7 commented on May 29, 2024

+1 yes, please

from grape.

niedhui avatar niedhui commented on May 29, 2024

Hi, I want to implement this very old feature, here is what I want at first (as @mbleigh said):

  auth { return a user}

  resource :post, auth: true do 
    get :id,  auth: false do
    end
  end

  post :something, auth: true do
  end

and provide the current_user help method (later this can be configed).

Because the auth method name was already taken, so I should find another name for it.
Just like devise, which provide authenticate & authenticate! method, still thinking how to implements this on the options way. maybe make the auth accept true, false and :optional.

I want to implement the block authenticate way later because if using with namespace ,there will be more block.

class API < Grape::API
  authenticate do 
     resource :post do
       get :id do
       end
     end
  end
end

any suggestion?

from grape.

dblock avatar dblock commented on May 29, 2024

Maybe a clearer name could be protect do, because there's a combination of authenticate and authorize that's going to be going on most likely.

from grape.

niedhui avatar niedhui commented on May 29, 2024

Hi, I started working on this yesterday.
the current DSL is like this:

  protection do
    # this method should return a user or nil, 
    # the return value will set to ['api.current_user'] and access by current_user
    # it can access params and headers
  end

  post :post, protect: true do

  end

I implement this by adding a middleware , but found the params and headers methods of Endpoint are only available after building middleware . so maybe I should change Protection to a simple class, add the protection call to
Endpoint #run just like the validation ?

and about eval the protection block, I'm using Protection#instance_eval , so I can add some help methods like http_basic that can be used in the block, but this need to delegate some methods to the endpoint, like params, headers, cookies.
If using Endpoint#instance_eval, there will be lack of adding custom help methods.

from grape.

dblock avatar dblock commented on May 29, 2024

We now have pretty extensive support for custom auth middleware, see https://github.com/intridea/grape#authentication. I am going to make an executive decision by saying that any protection (aka authorization) implementation that decorates individual APIs in a way that says, for example, "this API requires admin privileges", aka role-based auth, should be delegated into a separate gem.

from grape.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.