Coder Social home page Coder Social logo

go_rest_test's Introduction

go-rest-test

Simple REST API demo server written on Go (golang) uses Cassandra as storage. Demonstrates base authorization and authentication with token.

A client can register new user, log in, call authorization required test function, and log out.

Used libraries:

API methods:

Request:

HTTP Method: POST. No authorization required.

Request payload: JSON

{
    "login" : "new_login_here",
    "password" : "new_password_here"
}

Response:

  • HTTP 200: OK. User registered.

Body: { "status" : "OK" }

  • HTTP 400: Bad request. Invalid input JSON; Login is empty; Password is empty; User is already registered.

Body: { "Error" : "Error message here" }

Request:

HTTP Method: POST. Basic access authentication required (https://en.wikipedia.org/wiki/Basic_access_authentication). Use early registered user login/password.

Request payload: none.

Response:

  • HTTP 200: OK. User logged in.

Body (sample):

{
    "access_token" : "LcJWsc6Fu4bcWgd4ZkCUEQ==",
	"exp_time" : "2015-09-26T21:22:40.1764963+03:00"
}

You will get a token and datetime when the token will expire.

  • HTTP 400: Bad request. Invalid authentication.

Body: { "Error" : "Invalid authentication" }

Request:

HTTP Method: GET. Token access authentication required (http://www.w3.org/2001/sw/Europe/events/foaf-galway/papers/fp/token_based_authentication/). Use early obtained token.

Request payload: none.

Response:

  • HTTP 200: OK. User is authenticated.

Body: {"authed" : "some username here" } You will get a login for provided token.

  • HTTP 401: Unauthorized. Invalid authentication token or token expired.

Body: { "Error" : "Not authorized" }

Request:

HTTP Method: POST. Token access authentication required. Use early obtained token.

Request payload: none.

Response:

  • HTTP 200: OK. User is logged out.

Body: {"status" : "OK"}

  • HTTP 401: Unauthorized. Invalid authentication token or token expired.

Body: { "Error" : "Not authorized" }

Curl demo:

1. Register user:

curl -i -X POST -d '{"login":"admin","password":"123"}' -H "Content-Type:application/json" http://localhost:8080/register

You should get result:

HTTP/1.1 200 OK
...
{
    "status" : "OK"
}

2. Login using basic auth

"YWRtaW46MTIz" - base64 for "admin:123"

curl -i -X POST -H "Authorization: Basic YWRtaW46MTIz" http://localhost:8080/login

You will get an access token. The result should look like:

HTTP/1.1 200 OK
...
{
    "access_token" : "LcJWsc6Fu4bcWgd4ZkCUEQ==",
	"exp_time" : "2015-09-26T21:22:40.1764963+03:00"
}

3. Testing that we are authorized using this token:

curl -i -H "Authorization: Token LcJWsc6Fu4bcWgd4ZkCUEQ==" http://localhost:8080/auth_test

You should get result:

HTTP/1.1 200 OK
...
{
    "authed" : "admin"
}

If you are not authorized or token is expired the response will be:

HTTP/1.1 401 Unauthorized
...

4. Logging out:

curl -i -X POST -H "Authorization: Token LcJWsc6Fu4bcWgd4ZkCUEQ==" http://localhost:8080/logout

You should get result:

HTTP/1.1 200 OK
...
{
    "status" : "OK"
}

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.