Coder Social home page Coder Social logo

xiaonuogantan / accesstoken Goto Github PK

View Code? Open in Web Editor NEW
0.0 2.0 2.0 158 KB

A martini middleware that handles access serialization / de-serialization before request handling and it also attaches a function to the request object which can be called by the actual view handler function to retrieve the de-serialized access token object.

Go 100.00%

accesstoken's Introduction

accesstoken

A martini middleware that handles access serialization / de-serialization before request handling and it also attaches a function to the request object which can be called by the actual view handler function to retrieve the de-serialized access token object.

Example

import (
	"github.com/go-martini/martini"
	"net/http"
	"net/http/httptest"
	"testing"
	"time"
)

func GetHome(authContext AuthContext, parms martini.Params) (int, string) {
	out, err := authContext.GetAccessTokenData()
	if err != nil {
		return http.StatusBadRequest, err.Error()
	}
	if err == nil {
		if userID, ok := out["userID"].(string); ok {
			if userID != "123456" {
				return http.StatusForbidden, "userID incorrect"
			}
		} else {
			return http.StatusInternalServerError, "userID not processable"
		}
		if version, ok := out["version"].(int64); ok {
			if version != 0 {
				return http.StatusForbidden, "version incorrect"
			}
		} else {
			return http.StatusInternalServerError, "version not processable"
		}
		return http.StatusOK, "Got Home"
	}
	return http.StatusBadRequest, err.Error()
}

func Test_AccessToken(t *testing.T) {
	recorder := httptest.NewRecorder()
	m := martini.New()
	m.Use(AttachAuthContext(
		"secret", "X-Browzoo-Authorization",
	))
	r := martini.NewRouter()
	r.Get("/", GetHome)
	m.Action(r.Handle)

	// Make an access token that expires in 10 seconds in the future
	accessToken := GenerateUserIDAndExpiryTimeAccessToken(
		[]byte("123456"), time.Duration(10*time.Second), "secret", 0,
	)
	req, _ := http.NewRequest("GET", "/", nil)
	req.Header.Add("X-Browzoo-Authorization", string(accessToken))
	m.ServeHTTP(recorder, req)
	if recorder.Code != http.StatusOK {
		t.Error("ResponseRecorder.Code not 200; it's ", recorder.Code)
		t.Error("ResponseRecorder.Body ", recorder.Body)
	}

	// Make an access token that expires in 10 seconds in the past
	recorder = httptest.NewRecorder()
	accessToken = GenerateUserIDAndExpiryTimeAccessToken(
		[]byte("123456"), time.Duration(-10*time.Second), "secret", 0,
	)
	req, _ = http.NewRequest("GET", "/", nil)
	req.Header.Add("X-Browzoo-Authorization", string(accessToken))
	m.ServeHTTP(recorder, req)
	if recorder.Code != http.StatusBadRequest {
		t.Error("ResponseRecorder.Code not 400; it's ", recorder.Code)
		t.Error("ResponseRecorder.Body ", recorder.Body)
	}
}

accesstoken's People

Contributors

xiaonuogantan avatar

Watchers

 avatar  avatar

Forkers

infinityb wolfias

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.