Browse Source

Add more code to indieauth

master
Peter Stuifzand 3 years ago
parent
commit
c9290a051d
  1. 90
      auth.go

90
auth.go

@ -4,10 +4,13 @@ import (
"context"
"encoding/json"
"fmt"
"io"
"log"
"net"
"net/http"
"net/url"
"os"
"strings"
"linkheader"
@ -15,6 +18,10 @@ import (
"willnorris.com/go/microformats"
)
type AuthResponse struct {
Me string `json:"me"`
}
type Endpoints struct {
AuthorizationEndpoint string `json:"authorization_endpoint"`
TokenEndpoint string `json:"token_endpoint"`
@ -183,3 +190,86 @@ func CreateAuthorizationURL(authURL url.URL, meURL, clientID, redirectURI, state
authURL.RawQuery = q.Encode()
return authURL.String()
}
func VerifyAuthCode(code, redirectURI, authEndpoint string) (bool, *AuthResponse, error) {
const ClientID = "https://p83.nl/track-me"
reqData := url.Values{}
reqData.Set("code", code)
reqData.Set("client_id", ClientID)
reqData.Set("redirect_uri", redirectURI)
req, err := http.NewRequest(http.MethodPost, authEndpoint, strings.NewReader(reqData.Encode()))
if err != nil {
return false, nil, err
}
req.Header.Add("Accept", "application/json")
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
client := http.Client{}
resp, err := client.Do(req)
if err != nil {
return false, nil, err
}
defer resp.Body.Close()
if resp.StatusCode == 200 {
input := io.TeeReader(resp.Body, os.Stderr)
dec := json.NewDecoder(input)
var authResponse AuthResponse
err = dec.Decode(&authResponse)
if err != nil {
return false, nil, err
}
return true, &authResponse, nil
}
return false, nil, fmt.Errorf("HTTP response code from authorization_endpoint (%s) %d", authEndpoint, resp.StatusCode)
}
type App struct {
Name string
IconURL string
}
func getPropString(mf *microformats.Microformat, prop string) string {
if v, e := mf.Properties[prop]; e {
if len(v) > 0 {
if val, ok := v[0].(string); ok {
return val
}
}
}
return ""
}
func GetAppInfo(clientID string) (App, error) {
var app App
clientURL, err := url.Parse(clientID)
if err != nil {
return app, err
}
resp, err := http.Get(clientID)
if err != nil {
return app, err
}
defer resp.Body.Close()
md := microformats.Parse(resp.Body, clientURL)
if len(md.Items) > 0 {
mf := md.Items[0]
if mf.Type[0] == "h-x-app" || mf.Type[0] == "h-app" {
app.Name = getPropString(mf, "name")
app.IconURL = getPropString(mf, "logo")
}
}
return app, nil
}

Loading…
Cancel
Save