1
0
Fork 0
mirror of https://github.com/Luzifer/vault-otp-ui.git synced 2024-11-09 16:50:05 +00:00
vault-otp-ui/oauth.go

35 lines
797 B
Go
Raw Normal View History

package main
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"net/url"
)
func getAuthenticationURL() string {
return fmt.Sprintf("http://github.com/login/oauth/authorize?client_id=%s&scope=read:org user", cfg.Github.ClientID)
}
func getAccessToken(code string) (string, error) {
v := url.Values{}
v.Set("client_id", cfg.Github.ClientID)
v.Set("client_secret", cfg.Github.ClientSecret)
v.Set("code", code)
buf := bytes.NewReader([]byte(v.Encode()))
req, _ := http.NewRequest("POST", "https://github.com/login/oauth/access_token", buf)
req.Header.Set("Accept", "application/json")
resp, err := http.DefaultClient.Do(req)
if err != nil {
return "", err
}
defer resp.Body.Close()
r := map[string]string{}
return r["access_token"], json.NewDecoder(resp.Body).Decode(&r)
}