dggoauth

package module
v0.0.0-...-8e8208e Latest Latest
Warning

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

Go to latest
Published: Dec 17, 2019 License: MIT Imports: 7 Imported by: 0

README

dggoauth

this package implements the oauth flow from here

Example

package main

import (
	"net/http"

	"github.com/gin-gonic/gin"
	"github.com/tensei/dggoauth"
)

func main() {

    // ClientID and ClientSecret from here
    // https://www.destiny.gg/profile/developer
    dgg, err := dggoauth.NewClient(&dggoauth.Options{
        ClientID:     "xxxxxxxxxxxxxxxx", // required
        ClientSecret: "xxxxxxxxxxxxxxxx", // required
        RedirectURI:  "https://linktomysites/callback", // required
    })
    if err != nil {
        panic(err)
    }

    stateVerifierStore := make(map[string]string)

    r := gin.Default()
    r.GET("/login", func(c *gin.Context) {
        state := "randomlyGeneratedState"
        redirectURL, verifier := dgg.GetAuthorizationURL(state)

        // store state and verifier together
        stateVerifierStore[state] = verifier

        // redirect the user to the redirectURL
        c.Redirect(http.StatusFound, redirectURL)
    })

    // after they login they get sent back to your redirect URL
    r.GET("/callback", func(c *gin.Context) {
        // same state you provided above
        state := c.Query("state")
        code := c.Query("code")

        // get your verifier from the store
        verifier := stateVerifierStore[state]

        // get the access token
        at, err := dgg.GetAccessToken(code, verifier)
        if err != nil {
            panic(err)
        }

        // get userinfo with access token or whatever

        c.Redirect(http.StatusFound, "/")
    })
    r.Run()
}

Documentation

Index

Constants

View Source
const (
	// AuthURL ...
	AuthURL = "https://www.destiny.gg/oauth/authorize"
	// TokenURL ...
	TokenURL = "https://www.destiny.gg/oauth/token"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type AccessTokenResponse

type AccessTokenResponse struct {
	AccessToken  string `json:"access_token"`
	ExpiresIn    int    `json:"expires_in"`
	RefreshToken string `json:"refresh_token"`
	Scope        string `json:"scope"`
	TokenType    string `json:"token_type"`
}

AccessTokenResponse ...

type Client

type Client struct {
	// contains filtered or unexported fields
}

Client ...

func NewClient

func NewClient(options *Options) (*Client, error)

NewClient ...

func (*Client) GetAccessToken

func (c *Client) GetAccessToken(code, verifier string) (*AccessTokenResponse, error)

GetAccessToken returns the access token with the code and verifier provided

func (*Client) GetAuthorizationURL

func (c *Client) GetAuthorizationURL(state string) (url string, verifier string)

GetAuthorizationURL returns the authorization url you need to redirect the user to

func (*Client) RenewAccessToken

func (c *Client) RenewAccessToken(refreshToken string) (*AccessTokenResponse, error)

RenewAccessToken renews the access token with the refresh token provided

type OauthError

type OauthError struct {
	Error   string `json:"error"`
	Message string `json:"message"`
	Code    int    `json:"code"`
}

OauthError ...

type Options

type Options struct {
	ClientID     string
	ClientSecret string
	RedirectURI  string
	HTTPClient   *http.Client
}

Options ...

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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