package oauth2

import "github.com/oslokommune/okctl/pkg/oauth2"

Package oauth2 implements the device flow authentication.

Most of this functionality is shamelessly taken from, with some modifications: - https://github.com/rjw57/oauth2device

Copyright (c) 2014, Rich Wareham rich.oauth2device@richwareham.com All rights reserved.

redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

  1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
  2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.

This software is provided by the copyright holders and contributors "as is" and any express or implied warranties, including, but not limited to, the implied warranties of merchantability and fitness for a particular purpose are disclaimed. in no event shall the copyright holder or contributors be liable for any direct, indirect, incidental, special, exemplary, or consequential damages (including, but not limited to, procurement of substitute goods or services; loss of use, data, or profits; or business interruption) however caused and on any theory of liability, whether in contract, strict liability, or tort (including negligence or otherwise) arising in any way out of the use of this software, even if advised of the possibility of such damage.

Index

Variables

var ErrAccessDenied = errors.New("access denied by user")

ErrAccessDenied is an error returned when the user has denied this app access to their account.

Functions

func WaitForDeviceAuthorization

func WaitForDeviceAuthorization(client HTTPClient, config *Config, code *DeviceCode) (*oauth2.Token, error)

WaitForDeviceAuthorization polls the token URL waiting for the user to authorize the app. Upon authorization, it returns the new token. If authorization fails then an error is returned. If that failure was due to a user explicitly denying access, the error is ErrAccessDenied.

Modified to work with: https://docs.github.com/en/developers/apps/authorizing-oauth-apps#device-flow

Types

type Config

type Config struct {
	*oauth2.Config
	DeviceEndpoint DeviceEndpoint
}

Config is a version of oauth2.Config augmented with device endpoints

type DeviceCode

type DeviceCode struct {
	DeviceCode      string `json:"device_code"`
	UserCode        string `json:"user_code"`
	VerificationURI string `json:"verification_uri"`
	ExpiresIn       int64  `json:"expires_in"`
	Interval        int64  `json:"interval"`
}

A DeviceCode represents the user-visible code, verification URL and device-visible code used to allow for user authorisation of this app. The app should show UserCode and VerificationURI to the user.

func RequestDeviceCode

func RequestDeviceCode(client HTTPClient, config *Config) (*DeviceCode, error)

RequestDeviceCode will initiate the OAuth2 device authorization flow. It requests a device code and information on the code and URL to show to the user. Pass the returned DeviceCode to WaitForDeviceAuthorization.

type DeviceEndpoint

type DeviceEndpoint struct {
	CodeURL string
}

DeviceEndpoint contains the URLs required to initiate the OAuth2.0 flow for a provider's device flow.

type HTTPClient

type HTTPClient interface {
	Do(req *http.Request) (*http.Response, error)
}

HTTPClient defines the required http client interface

Source Files

oauth2.go

Version
v0.0.106 (latest)
Published
Oct 21, 2022
Platform
linux/amd64
Imports
12 packages
Last checked
18 hours ago

Tools for package owners.