Giter Site home page Giter Site logo

tokenauth's Introduction

Token Auth (WIP)

A simple token authentication / custom JSON api enabling plugin.

Setup

Clone this repo into craft/plugins/tokenauth.

public/index.php needs

header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Headers: Authorization");

at the top. // TODO: make it so only API files need this

Apache users also need to add the following to their public/.htaccess file

RewriteEngine On
RewriteCond %{HTTP:Authorization} ^(.*)
RewriteRule .* - [e=HTTP_AUTHORIZATION:%1]

because Apache currently removes the header if it's not base46 encoded username:password.

Template Tags

{% requireJwt %} Template requires a valid JWT token (sent as the Authorization header Authorization: Bearer [jwt]) to access / use.

{% tokenAuthLogin %} Turn template into a login point. POSTing a loginName and password will return a JWT, or error if one occurs. Attempting to access the template via non-POST means will result in a 400 error. Ideally you'll want your login route SSL'd as (at the moment) you have to send the password as plain text.

{% tokenAuthPost %} Catch-all POST request handler. Routes requests according to their specified action (act in the post request). Will throw a 400 error if not accessed via POST. Requires a valid JWT in the Authorization header. You can use the same forms as you would on the front-end of a Craft site, except you'll need to switch out the action input for ```act``.

{% returnJson %} Sets the Content-Type header to application/json.

Example Get API

{% requireJwt %}
{% returnJson %}
{% set data = {'tokenValid':'true'} %}
{{ data|json_encode()|raw }}

TODO

  • Make it just generally less shit.
  • Test with CORS / CSRF (currently only tested with CSRF disabled)

tokenauth's People

Contributors

tam avatar tamfoolery avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  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.