Code Monkey home page Code Monkey logo

blog's Introduction

My first blog web-application using Ruby on Rails Tutorial

codecov Code Climate #StandWithUkraine

Link: https://hello-blog.herokuapp.com/

Thanks to: Ruby on Rails Tutorial Michael Hartl.

API documentation

GraphQL API documentation should be available at /graphql-docs relative path after executing next rake tasks:

  • rake graphql:dump_schema
  • rake graphql:generate_docs

Also, you are allowed to play with GraphiQL IDE in the development environment, which is mounted by default at /graphiql.

Authentication

Authentication is built using regular tokens saved in the separate authentication_tokens DB table. Unfortunately, JSON Web Tokens (JWT) are not suitable for us, because we want to give the user the opportunity to invalidate their token, view and manage all of their sessions at any time. Tokens themselves are saved using SHA256 hash function with random salt, which is set via SECRET_ENCRYPTOR_KEY_SALT environment variable.

Security side

You may notice, that tokens aren't securely compared and are probably vulnerable to timing attacks (example: an attacker computes tons of hashes and stores them looking for a hash that starts with 0x00. Upon finding a plaintext with that value, they send that plaintext, they listen for timing on response. They do this for 255 more values until one value takes slightly longer. That's the first byte value of the hash created from the plaintext), but...

This will almost certainly not matter because to carry out the attack, the attacker must submit tokens whose hash value they know. The hash value depends on the salt. Thus, unless the attacker somehow already knows the salt, carrying out this attack is not possible.

Thus, while there's indeed a possibility of such an attack, it's extremely infeasible and it's not our case. The computation requirements for such an attack are also enormous, and they keep growing exponentially with every bit discovered from the hash.

Authentication usage example

  1. Call API to sign the user up:
  • curl -X POST -H "Content-Type: application/json" --data '{ "query": "mutation { signUp(email: \"[email protected]\", password: \"12345678\", passwordConfirmation: \"12345678\") { user { id email } token }}" }' localhost:3000/graphql

  • Expected response: {"data":{"signUp":{"user":{"id":"14","email":"[email protected]"},"token":"fPP4M97tEBnuHX3FK6K6jF32"}}}

  1. Save the token and next time include its value with X_AUTH_TOKEN header to call all protected endpoints:
  • curl -X POST -H "Content-Type: application/json" -H "X_AUTH_TOKEN: fPP4M97tEBnuHX3FK6K6jF32" --data '{ "query": "{ me { id email }}" }' localhost:3000/graphql

  • Expected response (with X_AUTH_TOKEN header): {"data":{"me":{"id":"14","email":"[email protected]"}}}

  • Expected response (without provided auth token): {"data":{"me":null},"errors":[{"message":"You need to authenticate to perform this action.","locations":[{"line":1,"column":3}],"path":["me"]}]}

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.