GithubHelp home page GithubHelp logo

isabella232 / osincli Goto Github PK

View Code? Open in Web Editor NEW

This project forked from openshift/osincli

0.0 0.0 0.0 20 KB

Golang OAuth2 client library

License: BSD 3-Clause "New" or "Revised" License

Go 100.00%

osincli's Introduction

OSIN CLIENT

Golang OAuth2 client library

OSINCLI is an OAuth2 client library for the Go language, as specified at http://tools.ietf.org/html/rfc6749.

Using it, you can access an OAuth2 authenticated service.

The library follows the RFC recommendations, but allows some differences, like passing the client secret in the url instead of using the Authorization header.

Example

import "github.com/RangelReale/osincli"

config := &osincli.ClientConfig{
	ClientId:                 "xxxxxxxxxxxx.apps.googleusercontent.com",
	ClientSecret:             "secret",
	AuthorizeUrl:             "https://accounts.google.com/o/oauth2/auth",
	TokenUrl:                 "https://accounts.google.com/o/oauth2/token",
	RedirectUrl:              "http://localhost:14001/appauth",
	ErrorsInStatusCode:       true,
	SendClientSecretInParams: true,
	Scope: "https://www.googleapis.com/auth/plus.login",
}
client, err := osincli.NewClient(config)
if err != nil {
	panic(err)
}

// create a new request to generate the url
areq := client.NewAuthorizeRequest(osincli.CODE)
areq.CustomParameters["access_type"] = "online"
areq.CustomParameters["approval_prompt"] = "auto"

// Home
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
	u := areq.GetAuthorizeUrl()
	w.Write([]byte(fmt.Sprintf("<a href=\"%s\">Login</a>", u.String())))
})

// Auth endpoint
http.HandleFunc("/appauth", func(w http.ResponseWriter, r *http.Request) {
	// parse a token request
	if areqdata, err := areq.HandleRequest(r); err == nil {
		treq := client.NewAccessRequest(osincli.AUTHORIZATION_CODE, areqdata)

		// exchange the authorize token for the access token
		ad, err := treq.GetToken()
		if err == nil {
			w.Write([]byte(fmt.Sprintf("Access token: %+v\n\n", ad)))
			
			// use the token in ad.AccessToken
		} else {
			w.Write([]byte(fmt.Sprintf("ERROR: %s\n", err)))
		}
	} else {
		w.Write([]byte(fmt.Sprintf("ERROR: %s\n", err)))
	}
})

http.ListenAndServe(":14001", nil)

Load your web browser at:

http://localhost:14001

License

The code is licensed using "New BSD" license.

Author

Rangel Reale

osincli's People

Contributors

enj avatar liggitt avatar nimajalali avatar rangelreale 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.