# README
libgithubauth v1.0.0
A GitHub auth library for Golang.
Usage
Put this in your go.mod
file
require github.com/BlyatManGopnik/libgithubauth v1.0.0
Documentation
Init()
Init contacts the API endpoint https://github.com/login/device/code to get a device-code needed for getting an auth-token and a user-code, which is what you enter at https://github.com/login/device to get an auth-token.
Example
package main
import (
"github.com/BlyatManGopnik/libgithubauth/headless"
"log"
"fmt"
)
func main() {
// Create an OAuth app using these instructions: https://docs.github.com/en/free-pro-team@latest/developers/apps/creating-an-oauth-app
clientid := "CLIENTID"
// Get scope names from here: https://docs.github.com/en/free-pro-team@latest/developers/apps/scopes-for-oauth-apps#available-scopes
// You may use more then one scope at once by seperating the scope names with a comma, like this "repo, gist"
scope := "scope"
devicecode, usercode, err := headless.Init(clientid, scope)
if err != nil {
log.Fatal(err)
}
fmt.Println(devicecode)
fmt.Println(usercode)
}
GetAuthToken()
GetAuthToken contacts the API endpoint https://github.com/login/oauth/access_token to check if you entered the user-code and if you entered the user-code, it gets the token generated by GitHub. Run this function every 5 seconds until you get the auth token so you don't spam the API. The function will return an error if it detects you havent put the user-code into the GitHub Login page.
Example
package main
import (
"github.com/BlyatManGopnik/libgithubauth/headless"
"log"
"fmt"
"time"
)
func main() {
var done bool
var token string
// Create an OAuth app using these instructions: https://docs.github.com/en/free-pro-team@latest/developers/apps/creating-an-oauth-app
clientid := "CLIENTID"
// Get scope names from here https://docs.github.com/en/free-pro-team@latest/developers/apps/scopes-for-oauth-apps#available-scopes
// You may use more then one scope at once by seperating the scope names with a comma, like this "repo, gist"
scope := "scope"
devicecode, usercode, err := headless.Init(clientid, scope)
if err != nil {
log.Fatal(err)
}
// Show the user the user-code so they can enter it at the GitHub device page
fmt.Println("Please enter this code at https://github.com/login/device\n" + usercode)
// This code is dumb but it works
for !done {
var done2 bool
done2 = false
for !done2 {
authtoken, err := headless.GetAuthToken(clientid, devicecode)
if err != nil {
if fmt.Sprint(err) == "access_denied" {
log.Fatal("Access has been denied")
} else {
time.Sleep(time.Second * 5)
done2 = true
}
} else {
token = authtoken
done = true
done2 = true
}
}
}
fmt.Println(token)
}
Licence
This documentation and this code is licenced unde the Apache 2.0 Licence.
Copyright 2020 BlyatManGopnik
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
# Packages
Package headless contains the functions to authenticate with GitHub.