1
0
mirror of https://github.com/Luzifer/vault-otp-ui.git synced 2024-09-16 15:48:32 +00:00
vault-otp-ui/oauth.go
Knut Ahlers 154562e961
Initial version
Signed-off-by: Knut Ahlers <knut@ahlers.me>
2017-06-14 20:46:35 +02:00

35 lines
797 B
Go

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)
}