Refactor forgejo user interaction
This commit is contained in:
parent
a01f6dec23
commit
a1c5827641
2 changed files with 16 additions and 27 deletions
|
|
@ -6,17 +6,12 @@ import (
|
||||||
"encoding/base64"
|
"encoding/base64"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"forgejo.gwairfelin.com/max/gonotes/internal/conf"
|
"forgejo.gwairfelin.com/max/gonotes/internal/conf"
|
||||||
"golang.org/x/oauth2"
|
"golang.org/x/oauth2"
|
||||||
)
|
)
|
||||||
|
|
||||||
type userInfo struct {
|
|
||||||
preferred_username string
|
|
||||||
}
|
|
||||||
|
|
||||||
func GenerateStateOAUTHCookie(w http.ResponseWriter, prefix string) string {
|
func GenerateStateOAUTHCookie(w http.ResponseWriter, prefix string) string {
|
||||||
|
|
||||||
b := make([]byte, 16)
|
b := make([]byte, 16)
|
||||||
|
|
@ -31,23 +26,23 @@ func GenerateStateOAUTHCookie(w http.ResponseWriter, prefix string) string {
|
||||||
return state
|
return state
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetUserData(code string, oauth *oauth2.Config) (map[string]any, error) {
|
func GetUserFromForgejo(code string, oauth *oauth2.Config) (string, error) {
|
||||||
// Use code to get token and get user info from Google.
|
// Use code to get token and get user info from Google.
|
||||||
|
|
||||||
token, err := oauth.Exchange(context.Background(), code)
|
token, err := oauth.Exchange(context.Background(), code)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("code exchange wrong: %s", err.Error())
|
return "", fmt.Errorf("code exchange wrong: %s", err.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
request, err := http.NewRequest("GET", conf.Conf.OIDC.UserinfoURL, nil)
|
request, err := http.NewRequest("GET", conf.Conf.OIDC.UserinfoURL, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("failed to init http client for userinfo: %s", err.Error())
|
return "", fmt.Errorf("failed to init http client for userinfo: %s", err.Error())
|
||||||
}
|
}
|
||||||
request.Header.Set("Authorization", fmt.Sprintf("token %s", token.AccessToken))
|
request.Header.Set("Authorization", fmt.Sprintf("token %s", token.AccessToken))
|
||||||
response, err := http.DefaultClient.Do(request)
|
response, err := http.DefaultClient.Do(request)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("failed getting user info: %s", err.Error())
|
return "", fmt.Errorf("failed getting user info: %s", err.Error())
|
||||||
}
|
}
|
||||||
defer response.Body.Close()
|
defer response.Body.Close()
|
||||||
|
|
||||||
|
|
@ -55,10 +50,17 @@ func GetUserData(code string, oauth *oauth2.Config) (map[string]any, error) {
|
||||||
|
|
||||||
err = json.NewDecoder(response.Body).Decode(&uInf)
|
err = json.NewDecoder(response.Body).Decode(&uInf)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("failed to parse response as json: %s", err.Error())
|
return "", fmt.Errorf("failed to parse response as json: %s", err.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Printf("Contents of user data response %s", uInf)
|
username, ok := uInf["preferred_username"]
|
||||||
|
if !ok {
|
||||||
|
return "", fmt.Errorf("no username in response: %s", err.Error())
|
||||||
|
}
|
||||||
|
userStr, ok := username.(string)
|
||||||
|
if !ok {
|
||||||
|
return "", fmt.Errorf("username not a string: %s", err.Error())
|
||||||
|
}
|
||||||
|
|
||||||
return uInf, nil
|
return userStr, nil
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -97,27 +97,14 @@ func (s *SessionStore) CallbackViewOAUTH(w http.ResponseWriter, r *http.Request)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
data, err := auth.GetUserData(r.FormValue("code"), s.oauth)
|
username, err := auth.GetUserFromForgejo(r.FormValue("code"), s.oauth)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println(err.Error())
|
log.Println(err.Error())
|
||||||
http.Redirect(w, r, "/", http.StatusTemporaryRedirect)
|
http.Redirect(w, r, "/", http.StatusTemporaryRedirect)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
username, ok := data["preferred_username"]
|
s.Login(username, w)
|
||||||
if !ok {
|
|
||||||
log.Println("No username in auth response")
|
|
||||||
http.Redirect(w, r, "/", http.StatusTemporaryRedirect)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
userStr, ok := username.(string)
|
|
||||||
if !ok {
|
|
||||||
log.Println("Username not interpretable as string")
|
|
||||||
http.Redirect(w, r, "/", http.StatusTemporaryRedirect)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
s.Login(userStr, w)
|
|
||||||
http.Redirect(w, r, "/", http.StatusTemporaryRedirect)
|
http.Redirect(w, r, "/", http.StatusTemporaryRedirect)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue