Skip to content

Commit

Permalink
Merge pull request #104 from andy89923/fix/oauth2
Browse files Browse the repository at this point in the history
fix: missing token in header
  • Loading branch information
ianchen0119 authored Jun 28, 2024
2 parents 7458449 + 03db83c commit c731390
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 2 deletions.
11 changes: 9 additions & 2 deletions backend/WebUI/api_charging.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,13 +131,20 @@ func GetChargingRecord(c *gin.Context) {
return
}

res, err := http.NewRequestWithContext(ctx, http.MethodGet, requestUri, nil)
req, err := http.NewRequestWithContext(ctx, http.MethodGet, requestUri, nil)
if err != nil {
logger.ProcLog.Error(err)
c.JSON(http.StatusInternalServerError, gin.H{})
return
}
resp, res_err := httpsClient.Do(res)

if err = webui_context.GetSelf().RequestBindToken(req, ctx); err != nil {
logger.ProcLog.Error(err)
c.JSON(http.StatusInternalServerError, gin.H{})
return
}

resp, res_err := httpsClient.Do(req)
if res_err != nil {
logger.ProcLog.Error(err)
c.JSON(http.StatusInternalServerError, gin.H{})
Expand Down
6 changes: 6 additions & 0 deletions backend/WebUI/api_verify.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@ func GetSmfUserPlaneInfo() (interface{}, error) {
logger.ProcLog.Error(err_req)
return jsonData, err_req
}

if err = webui_context.GetSelf().RequestBindToken(req, ctx); err != nil {
logger.ProcLog.Error(err)
return jsonData, err
}

resp, err_rsp := httpsClient.Do(req)
if err_rsp != nil {
logger.ProcLog.Error(err_rsp)
Expand Down
7 changes: 7 additions & 0 deletions backend/WebUI/api_webui.go
Original file line number Diff line number Diff line change
Expand Up @@ -1800,6 +1800,13 @@ func GetRegisteredUEContext(c *gin.Context) {
c.JSON(http.StatusInternalServerError, gin.H{})
return
}

if err = webui_context.GetSelf().RequestBindToken(req, ctx); err != nil {
logger.ProcLog.Error(err)
c.JSON(http.StatusInternalServerError, gin.H{})
return
}

resp, err := httpsClient.Do(req)
if err != nil {
logger.ProcLog.Error(err)
Expand Down
19 changes: 19 additions & 0 deletions backend/webui_context/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@ package webui_context
import (
"context"
"fmt"
"net/http"

"github.com/google/uuid"
"golang.org/x/oauth2"

"github.com/free5gc/openapi"
"github.com/free5gc/openapi/Nnrf_NFDiscovery"
"github.com/free5gc/openapi/Nnrf_NFManagement"
"github.com/free5gc/openapi/models"
Expand Down Expand Up @@ -164,3 +167,19 @@ func (c *WEBUIContext) GetTokenCtx(serviceName models.ServiceName, targetNF mode
return oauth.GetTokenCtx(models.NfType_AF, targetNF,
c.NfInstanceID, c.NrfUri, string(serviceName))
}

// NewRequestWithContext() will not apply header in ctx
// so httpsClient.Do(req) will not have token in header if OAuth2 enable
func (c *WEBUIContext) RequestBindToken(req *http.Request, ctx context.Context) error {
if tok, ok := ctx.Value(openapi.ContextOAuth2).(oauth2.TokenSource); ok {
// We were able to grab an oauth2 token from the context
var latestToken *oauth2.Token
var err error
if latestToken, err = tok.Token(); err != nil {
logger.ConsumerLog.Error(err)
return err
}
latestToken.SetAuthHeader(req)
}
return nil
}

0 comments on commit c731390

Please sign in to comment.