ghoauth

package module
v0.0.0-...-2ccb6f0 Latest Latest
Warning

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

Go to latest
Published: Jul 17, 2014 License: ISC Imports: 7 Imported by: 3

README

go-ghoauth godoc

Provides easy two steps flow to create an access_token for your wonderfull github application.

wercker status

Example

example/webapp.go

package main

import (
  "github.com/vvo/go-ghoauth"
  "net/http"
)

var ghflow = ghoauth.New(&ghoauth.Config{
  ClientId:     "82c041166d565cf2f149",
  ClientSecret: "6458b989c06d8106f468fd47106d8654e3887d5b",
  RedirectUri:  "http://localhost:8080/callback",
  Scope:        "user:public",
})

func main() {
  // step 1: redirect users to the right github oauth page
  // https://developer.github.com/v3/oauth/#redirect-users-to-request-github-access
  http.HandleFunc("/login", ghflow.Login)
  http.HandleFunc("/callback", callback)
  
  http.HandleFunc("/", root)
  panic(http.ListenAndServe(":8080", nil))
}

func callback(w http.ResponseWriter, r *http.Request) {
  // step 2: github calls you using redirect_uri
  // we generate a token using github oauth access_token POST url
  // callback does not responds to client for you, it only generates a token
  // https://developer.github.com/v3/oauth/#github-redirects-back-to-your-site
  token, err := ghflow.Callback(r)

  if err != nil {
    w.Write([]byte("UhOh!"))
  }

  // token is now available and usable
  w.Write([]byte("Here's your access_token budy: " + token))
}

func root(w http.ResponseWriter, r *http.Request) {
  w.Write([]byte("<a href=/login>login</a>"))
}

Documentation

Overview

Package ghoauth provides acces_token creation for your github application

Two steps:

  1. redirect user to github auth page
  2. create an access_token

Example:

package main

import (
	"github.com/vvo/go-ghoauth"
	"net/http"
)

var ghflow = ghoauth.New(&ghoauth.Config{
	ClientId:     "82c041166d565cf2f149",
	ClientSecret: "6458b989c06d8106f468fd47106d8654e3887d5b",
	RedirectUri:  "http://localhost:8080/callback",
	Scope:        "user:public",
})

func main() {
	// step 1: redirect users to the right github oauth page
	// https://developer.github.com/v3/oauth/#redirect-users-to-request-github-access
	http.HandleFunc("/login", ghflow.Login)
	http.HandleFunc("/callback", callback)

	http.HandleFunc("/", root)
	panic(http.ListenAndServe(":8080", nil))
}

func callback(w http.ResponseWriter, r *http.Request) {
	// step 2: github calls you using redirect_uri
	// we generate a token using github oauth access_token POST url
	// callback does not responds to client for you, it only generates a token
	// https://developer.github.com/v3/oauth/#github-redirects-back-to-your-site
	token, err := ghflow.Callback(r)

	if err != nil {
		w.Write([]byte("UhOh!"))
	}

	// token is now available and usable
	w.Write([]byte("Here's your access_token budy: " + token))
}

func root(w http.ResponseWriter, r *http.Request) {
	w.Write([]byte("<a href=/login>login</a>"))
}

Index

Constants

This section is empty.

Variables

View Source
var ErrStateNotFound = errors.New("Provided `state` was not found")

refers to https://developer.github.com/v3/oauth/#github-redirects-back-to-your-site "If the states don’t match, the request has been created by a third party and the process should be aborted."

Functions

func New

func New(config *Config) *oauthFlow

Types

type Config

type Config struct {
	ClientId     string
	ClientSecret string

	RedirectUri string

	// Comma separated list of scopes https://developer.github.com/v3/oauth/#scopes
	Scope string

	// Defaults to defaultBaseUrl
	BaseUrl string
}

See https://developer.github.com/v3/oauth/#web-application-flow to understand how to fill the properties

type GithubError

type GithubError struct {
	RawError         string `json:"error"`
	ErrorDescription string `json:"error_description"`
	ErrorUri         string `json:"error_uri"`
}

func (*GithubError) Error

func (e *GithubError) Error() string

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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