-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #22 from TYuan0816/feature/oauth2
Feature/oauth2
- Loading branch information
Showing
14 changed files
with
213 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package util | ||
|
||
import ( | ||
"net/http" | ||
|
||
"github.com/gin-gonic/gin" | ||
|
||
ausf_context "github.com/free5gc/ausf/internal/context" | ||
"github.com/free5gc/ausf/internal/logger" | ||
"github.com/free5gc/openapi/models" | ||
) | ||
|
||
type RouterAuthorizationCheck struct { | ||
serviceName models.ServiceName | ||
} | ||
|
||
func NewRouterAuthorizationCheck(serviceName models.ServiceName) *RouterAuthorizationCheck { | ||
return &RouterAuthorizationCheck{ | ||
serviceName: serviceName, | ||
} | ||
} | ||
|
||
func (rac *RouterAuthorizationCheck) Check(c *gin.Context, ausfContext ausf_context.NFContext) { | ||
token := c.Request.Header.Get("Authorization") | ||
err := ausfContext.AuthorizationCheck(token, rac.serviceName) | ||
if err != nil { | ||
logger.UtilLog.Debugf("RouterAuthorizationCheck: Check Unauthorized: %s", err.Error()) | ||
c.JSON(http.StatusUnauthorized, gin.H{"error": err.Error()}) | ||
c.Abort() | ||
return | ||
} | ||
|
||
logger.UtilLog.Debugf("RouterAuthorizationCheck: Check Authorized") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
package util | ||
|
||
import ( | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
|
||
"github.com/gin-gonic/gin" | ||
"github.com/pkg/errors" | ||
|
||
"github.com/free5gc/openapi/models" | ||
) | ||
|
||
const ( | ||
Valid = "valid" | ||
Invalid = "invalid" | ||
) | ||
|
||
type mockAUSFContext struct{} | ||
|
||
func newMockAUSFContext() *mockAUSFContext { | ||
return &mockAUSFContext{} | ||
} | ||
|
||
func (m *mockAUSFContext) AuthorizationCheck(token string, serviceName models.ServiceName) error { | ||
if token == Valid { | ||
return nil | ||
} | ||
|
||
return errors.New("invalid token") | ||
} | ||
|
||
func TestRouterAuthorizationCheck_Check(t *testing.T) { | ||
// Mock gin.Context | ||
w := httptest.NewRecorder() | ||
c, _ := gin.CreateTestContext(w) | ||
|
||
var err error | ||
c.Request, err = http.NewRequest("GET", "/", nil) | ||
if err != nil { | ||
t.Errorf("error on http request: %+v", err) | ||
} | ||
|
||
type Args struct { | ||
token string | ||
} | ||
type Want struct { | ||
statusCode int | ||
} | ||
|
||
tests := []struct { | ||
name string | ||
args Args | ||
want Want | ||
}{ | ||
{ | ||
name: "Valid Token", | ||
args: Args{ | ||
token: Valid, | ||
}, | ||
want: Want{ | ||
statusCode: http.StatusOK, | ||
}, | ||
}, | ||
{ | ||
name: "Invalid Token", | ||
args: Args{ | ||
token: Invalid, | ||
}, | ||
want: Want{ | ||
statusCode: http.StatusUnauthorized, | ||
}, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
w = httptest.NewRecorder() | ||
c, _ = gin.CreateTestContext(w) | ||
c.Request, err = http.NewRequest("GET", "/", nil) | ||
if err != nil { | ||
t.Errorf("error on http request: %+v", err) | ||
} | ||
c.Request.Header.Set("Authorization", tt.args.token) | ||
|
||
rac := NewRouterAuthorizationCheck(models.ServiceName("testService")) | ||
rac.Check(c, newMockAUSFContext()) | ||
if w.Code != tt.want.statusCode { | ||
t.Errorf("StatusCode should be %d, but got %d", tt.want.statusCode, w.Code) | ||
} | ||
}) | ||
} | ||
} |
Oops, something went wrong.