Giter Site home page Giter Site logo

luizsanches / decidim-module-apiauth Goto Github PK

View Code? Open in Web Editor NEW

This project forked from mainio/decidim-module-apiauth

0.0 0.0 0.0 240 KB

API authentication module with JWT for Decidim.

License: GNU Affero General Public License v3.0

Ruby 100.00%

decidim-module-apiauth's Introduction

Decidim::Apiauth

Build Status codecov

A Decidim module to add JWT token based API authentication possibility to Decidim.

The API authentication module provides a new endpoint for API authentication and a method to check for an active authentication token header for each request.

Based on Devise::JWT.

The development has been sponsored by the City of Helsinki.

Installation

Add this line to your application's Gemfile:

gem "decidim-apiauth"

And then execute:

$ bundle
$ bundle exec rails decidim_apiauth:install:migrations
$ bundle exec rails db:migrate

Then, configure a secret key by adding the following to your application's config/secrets.yml:

development:
  <<: *default
  # ...
  secret_key_jwt: generate_a_key_here

test:
  <<: *default
  # ...
  secret_key_jwt: generate_a_key_here

# Do not keep production secrets in the repository,
# instead read values from the environment.
production:
  <<: *default
  # ...
  secret_key_jwt: <%= ENV["SECRET_KEY_JWT"] %>

You can generate the key from the console by running:

$ bundle exec rails secret
abcdef123456... <-- (This printed line is the secret)

Usage

  1. Login
curl --location --request POST 'http://localhost:3000/api/sign_in' \
--form 'user[email]="[email protected]"' \
--form 'user[password]="decidim123456"'
  1. Save jwt web token from response
  2. Include token to further requests
curl --location --request POST 'http://localhost:3000/api' \
--header 'Authorization: Bearer replace_this_with_token' \
--header 'Content-Type: application/json' \
--data-raw '{"query":"{\n  session {\nuser {\n id\n nickname\n}\n}\n}","variables":{}}'

Troubleshoot

In case you run into problems like getting: {"data":{"session":null}}

  1. Make sure you aren't logged already
  2. In production / staing check that request URL has https protocol (not http)!
  3. Make sure that request has Content-Type: application/json and Content-Length
  4. If you are using Postman, create clean new request
  5. Make sure that secrets (secrets.yml) are entered correctly: secret_key_jwt: <%= ENV["SECRET_KEY_JWT"] %> and that SECRET_KEY_JWT environment variable has been set.

Configuration

By default, API authentication is necessary if the Decidim organization is set to force authentication.

If you want to make API authentication necessary for public instances, you can use the following configuration option:

# config/initializers/decidim.rb
Decidim::Apiauth.configure do |config|
  config.force_api_authentication = true
end

Contributing

See Decidim.

Testing

To run the tests run the following in the gem development path:

$ bundle
$ DATABASE_USERNAME=<username> DATABASE_PASSWORD=<password> bundle exec rake test_app
$ DATABASE_USERNAME=<username> DATABASE_PASSWORD=<password> bundle exec rspec

Note that the database user has to have rights to create and drop a database in order to create the dummy test app database.

In case you are using rbenv and have the rbenv-vars plugin installed for it, you can add these environment variables to the root directory of the project in a file named .rbenv-vars. In this case, you can omit defining these in the commands shown above.

Test code coverage

If you want to generate the code coverage report for the tests, you can use the SIMPLECOV=1 environment variable in the rspec command as follows:

$ SIMPLECOV=1 bundle exec rspec

This will generate a folder named coverage in the project root which contains the code coverage report.

License

See LICENSE-AGPLv3.txt.

decidim-module-apiauth's People

Contributors

sinaeftekhar avatar ahukkanen avatar luizsanches avatar

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.