goth

package module
v0.0.0-...-ae202b5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 30, 2014 License: MIT Imports: 13 Imported by: 0

README

goth

Go OAuth 1.0a 2 legs provider with oauth_body_hash support.

Just provide a function conforms to OAuthCheckerFunc Goth will authenticate your requests.

You can get the userId return by your auth function inside the handler at r.URL.User.Username().

To enable oauth_body_hash check set WithBodyHash to true.

package main

import (
	"fmt"
	"net/http"

	"github.com/akhenakh/goth"
)

func myAuthFunc(consumerKey string) (userId string, consumerSecret string) {
	if consumerKey == "ckey" {
		return "myusername", "csecret"
	}
	return "", ""
}

func helloFunc(w http.ResponseWriter, r *http.Request) {
	fmt.Fprintf(w, "hello "+r.URL.User.Username())
}

func main() {
	o := goth.OAuth1{CheckerFunc: myAuthFunc}
	myHandler := http.HandlerFunc(helloFunc)
	http.ListenAndServe(":3000", o.AuthProtect(myHandler))
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// DefaultDeniedHandler handles the requests that were denied access because
	// of OAuth1. By default, returns a 401 status code with a generic message.
	DefaultDeniedHandler = http.Handler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		http.Error(w, "Unauthorized", 401)
	}))
)

Functions

This section is empty.

Types

type OAuth1

type OAuth1 struct {
	// A function that return the consumer secret and the username of the associated consumer key
	CheckerFunc OAuthCheckerFunc

	// DeniedHandler is called if the request is unauthorized. If it is nil,
	// the DefaultDeniedHandler variable is used.
	DeniedHandler http.Handler

	// WithBodyHash will check for the optionnal oauth_body_hash, default to no
	// see http://oauth.googlecode.com/svn'/spec/ext/body_hash/1.0/drafts/4/spec.html
	WithBodyHash bool
}

func (*OAuth1) AuthProtect

func (o *OAuth1) AuthProtect(h http.Handler) http.Handler

type OAuthCheckerFunc

type OAuthCheckerFunc func(consumerKey string) (userId string, consumerSecret string)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL