GithubHelp home page GithubHelp logo

david972 / oauth2cli Goto Github PK

View Code? Open in Web Editor NEW

This project forked from int128/oauth2cli

0.0 2.0 0.0 16 KB

Go library for better user experience on OAuth 2.0 and OpenID Connect (OIDC) on CLI

Home Page: https://godoc.org/github.com/int128/oauth2cli

License: Apache License 2.0

Go 100.00%

oauth2cli's Introduction

oauth2cli CircleCI

A Go library for better user experience on OAuth 2.0 and OpenID Connect (OIDC) on CLI. It allows simple and easy user interaction with Authorization Code Grant Flow and a local server.

See GoDoc.

TL;DR

import (
	"context"
	"log"

	"github.com/david972/oauth2cli"
	"golang.org/x/oauth2"
)

var endpoint = oauth2.Endpoint{
	AuthURL:  "https://example.com/oauth2/auth",
	TokenURL: "https://example.com/oauth2/token",
} 

func main() {
	ctx := context.Background()
	flow := oauth2cli.AuthCodeFlow{
		Config: oauth2.Config{
			ClientID:     "YOUR_CLIENT_ID",
			ClientSecret: "YOUR_CLIENT_SECRET",
			Endpoint:     endpoint,
			Scopes:       []string{"email"},
		},
	}
	token, err := flow.GetToken(ctx)
	if err != nil {
		log.Fatalf("Could not get a token: %s", err)
	}
    log.Printf("Got a token: %+v", token)
}

Why

Typical OAuth 2.0 Authorization Code Grant implementation requires complicated user interaction. For example,

  1. User runs the command on a terminal.
  2. User opens the URL shown on the command.
  3. User logs in to the provider.
  4. User copies the code shown on the browser.
  5. User pastes the code on the terminal.
  6. User can access to the API using the token.

By using oauth2cli, user interaction will be simple and easy as follows:

  1. User runs the command on a terminal.
  2. oauth2cli opens the browser automatically.
  3. User logs in to the provider.
  4. oauth2cli gets a token from the provider.
  5. User can access to the API using the token.

How it works

oauth2cli performs the following steps:

  1. Start a local server at the port.
  2. Open browser and navigate to the local server.
  3. Wait for user authorization.
  4. Receive a code via an authorization response (HTTP redirect).
  5. Exchange the code and a token.
  6. Return the code.

Contributions

This is an open source software licensed under Apache 2.0. Feel free to open issues and pull requests.

oauth2cli's People

Contributors

int128 avatar

Watchers

 avatar  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.