Skip to content

Commit

Permalink
feat(middleware): use github.com/dghubble/sling as http client
Browse files Browse the repository at this point in the history
release-as: 0.1.1
  • Loading branch information
MuXiu1997 committed Jan 26, 2023
1 parent 4c1eac9 commit 81f461f
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 77 deletions.
6 changes: 3 additions & 3 deletions internal/app/traefik-github-oauth-server/model/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ type ResponseGenerateOAuthPageURL struct {
}

type RequestRedirect struct {
RID string `form:"rid" binding:"required"`
Code string `form:"code" binding:"required"`
RID string `form:"rid" url:"rid" binding:"required"`
Code string `form:"code" url:"code" binding:"required"`
}

type RequestGetAuthResult struct {
RID string `form:"rid" binding:"required"`
RID string `form:"rid" url:"rid" binding:"required"`
}

type ResponseGetAuthResult struct {
Expand Down
106 changes: 32 additions & 74 deletions middleware_plugin.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
package traefik_github_oauth_plugin

import (
"bytes"
"context"
"crypto/rand"
"encoding/hex"
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"strings"

"github.com/dghubble/sling"
"github.com/muxiu1997/traefik-github-oauth-plugin/internal/app/traefik-github-oauth-server/model"
"github.com/muxiu1997/traefik-github-oauth-plugin/internal/pkg/constant"
"github.com/muxiu1997/traefik-github-oauth-plugin/internal/pkg/jwt"
Expand Down Expand Up @@ -139,99 +136,60 @@ func (p *TraefikGithubOauthPlugin) handleAuthRequest(rw http.ResponseWriter, req
}

func (p *TraefikGithubOauthPlugin) redirectToOAuthPage(rw http.ResponseWriter, req *http.Request) {
oAuthPageURL, err := p.generateOAuthPageURL(req)
oAuthPageURL, err := p.generateOAuthPageURL(getRawRequestUrl(req), p.getAuthURL(req))
if err != nil {
http.Error(rw, err.Error(), http.StatusInternalServerError)
return
}
http.Redirect(rw, req, oAuthPageURL, http.StatusFound)
}

func (p *TraefikGithubOauthPlugin) generateOAuthPageURL(originalReq *http.Request) (string, error) {
var request *http.Request
{
requestURL, err := url.Parse(p.apiBaseUrl)
if err != nil {
return "", err
}
requestURL = requestURL.JoinPath(constant.ROUTER_GROUP_PATH_OAUTH, constant.ROUTER_PATH_OAUTH_PAGE_URL)
request, err := http.NewRequest(http.MethodPost, requestURL.String(), nil)
if err != nil {
return "", err
}
request.Header.Add("Content-Type", "application/json")
if 0 < len(p.apiSecretKey) {
request.Header.Add(constant.HTTP_HEADER_AUTHORIZATION, fmt.Sprintf("%s %s", constant.AUTHORIZATION_PREFIX_TOKEN, p.apiSecretKey))
}
requestBody, err := json.Marshal(model.RequestGenerateOAuthPageURL{
RedirectURI: getRawRequestUrl(originalReq),
AuthURL: p.getAuthURL(originalReq),
})
if err != nil {
return "", err
}
request.Body = io.NopCloser(bytes.NewReader(requestBody))
func (p *TraefikGithubOauthPlugin) generateOAuthPageURL(redirectURI, authURL string) (string, error) {
reqBody := model.RequestGenerateOAuthPageURL{
RedirectURI: redirectURI,
AuthURL: authURL,
}

result := &model.ResponseGenerateOAuthPageURL{}
resp, err := http.DefaultClient.Do(request)
req := sling.New().Base(p.apiBaseUrl).Post(constant.ROUTER_GROUP_PATH_OAUTH + "/" + constant.ROUTER_PATH_OAUTH_PAGE_URL)
if 0 < len(p.apiSecretKey) {
req.Set(constant.HTTP_HEADER_AUTHORIZATION, fmt.Sprintf("%s %s", constant.AUTHORIZATION_PREFIX_TOKEN, p.apiSecretKey))
}
var respBody model.ResponseGenerateOAuthPageURL
var errRespBody model.ResponseError
_, err := req.BodyJSON(reqBody).Receive(&respBody, &errRespBody)
if err != nil {
return "", err
}
defer func(b io.ReadCloser) {
_ = b.Close()
}(resp.Body)
if resp.StatusCode == http.StatusUnauthorized {
return "", fmt.Errorf("invalid api secret key")
}
if resp.StatusCode != http.StatusCreated {
return "", fmt.Errorf("generateOAuthPageURL failed, status code: %d", resp.StatusCode)
}
if err = json.NewDecoder(resp.Body).Decode(result); err != nil {
return "", err
if 0 < len(errRespBody.Message) {
return "", fmt.Errorf("rpc failed, message: %s", errRespBody.Message)
}
return result.OAuthPageURL, nil
return respBody.OAuthPageURL, nil
}

func (p *TraefikGithubOauthPlugin) getAuthResult(rid string) (*model.ResponseGetAuthResult, error) {
var request *http.Request
{
requestURL, err := url.Parse(p.apiBaseUrl)
if err != nil {
return nil, err
}
requestURL = requestURL.JoinPath(constant.ROUTER_GROUP_PATH_OAUTH, constant.ROUTER_PATH_OAUTH_RESULT)
requestURLQuery := requestURL.Query()
requestURLQuery.Set(constant.QUERY_KEY_REQUEST_ID, rid)
requestURL.RawQuery = requestURLQuery.Encode()
request, err := http.NewRequest(http.MethodGet, requestURL.String(), nil)
if err != nil {
return nil, err
}
if 0 < len(p.apiSecretKey) {
request.Header.Add(constant.HTTP_HEADER_AUTHORIZATION, fmt.Sprintf("%s %s", constant.AUTHORIZATION_PREFIX_TOKEN, p.apiSecretKey))
}
req := sling.New().Base(p.apiBaseUrl).Get(constant.ROUTER_GROUP_PATH_OAUTH + "/" + constant.ROUTER_PATH_OAUTH_RESULT)
if 0 < len(p.apiSecretKey) {
req.Set(constant.HTTP_HEADER_AUTHORIZATION, fmt.Sprintf("%s %s", constant.AUTHORIZATION_PREFIX_TOKEN, p.apiSecretKey))
}

resp, err := http.DefaultClient.Do(request)
// req.QueryStruct seems to panic in yaegi
httpRequest, err := req.Request()
if err != nil {
return nil, err
}
defer func(b io.ReadCloser) {
_ = b.Close()
}(resp.Body)
if resp.StatusCode == http.StatusUnauthorized {
return nil, fmt.Errorf("invalid api secret key")
}
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("getAuthResult failed, status code: %d", resp.StatusCode)
}
result := &model.ResponseGetAuthResult{}
err = json.NewDecoder(resp.Body).Decode(result)
q := httpRequest.URL.Query()
q.Add(constant.QUERY_KEY_REQUEST_ID, rid)
httpRequest.URL.RawQuery = q.Encode()

var respBody model.ResponseGetAuthResult
var errRespBody model.ResponseError
_, err = req.Do(httpRequest, &respBody, &errRespBody)
if err != nil {
return nil, err
}
return result, nil
if 0 < len(errRespBody.Message) {
return nil, fmt.Errorf("rpc failed, message: %s", errRespBody.Message)
}
return &respBody, nil
}

func (p *TraefikGithubOauthPlugin) getAuthURL(originalReq *http.Request) string {
Expand Down

0 comments on commit 81f461f

Please sign in to comment.