Skip to content

Commit

Permalink
Improve authorizer
Browse files Browse the repository at this point in the history
  • Loading branch information
berejant committed Nov 8, 2023
1 parent efd7b7e commit bf1f868
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 43 deletions.
40 changes: 10 additions & 30 deletions ApiController.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"authorizer/dto"
"context"
"embed"
"encoding/json"
Expand Down Expand Up @@ -38,27 +39,6 @@ type ApiController struct {
countCache *countCache
}

type AuthOptionsClaims struct {
jwt.RegisteredClaims
Client string `form:"client" json:"client" binding:"required"`
ClientUserId string `form:"client_user_id" json:"clientUserId" binding:"required"`
RedirectUri string `form:"redirect_uri" json:"redirectUri,omitempty"`
KneuUserId uint `form:"-" json:"userId,omitempty"`
}

type Student struct {
Id uint
LastName string
FirstName string
MiddleName string
Gender events.Gender
}

type GetAuthUrlResponse struct {
AuthUrl string `json:"authUrl" binding:"required"`
ExpireAt time.Time `json:"expire" binding:"required"`
}

func (controller *ApiController) setupRouter() *gin.Engine {
router := gin.New()

Expand Down Expand Up @@ -91,7 +71,7 @@ func (controller *ApiController) getAuthUrl(c *gin.Context) {
var err error
var state string

authOptionsClaims := AuthOptionsClaims{}
authOptionsClaims := dto.AuthOptionsClaims{}
err = c.Bind(&authOptionsClaims)
expireAt := time.Now().Add(stateLifetime).Truncate(jwt.TimePrecision)
if err == nil {
Expand All @@ -105,7 +85,7 @@ func (controller *ApiController) getAuthUrl(c *gin.Context) {
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "Wrong request data"})
} else {
response := GetAuthUrlResponse{
response := dto.GetAuthUrlResponse{
AuthUrl: controller.oauthClient.GetOauthUrl(controller.oauthRedirectUrl, state),
ExpireAt: expireAt,
}
Expand All @@ -116,7 +96,7 @@ func (controller *ApiController) getAuthUrl(c *gin.Context) {

func (controller *ApiController) completeAuth(c *gin.Context) {

var authOptionsClaims AuthOptionsClaims
var authOptionsClaims dto.AuthOptionsClaims
var tokenResponse kneu.OauthTokenResponse
var userMeResponse kneu.UserMeResponse

Expand Down Expand Up @@ -170,7 +150,7 @@ func (controller *ApiController) completeAuth(c *gin.Context) {
return
}

err = controller.finishAuthorization(authOptionsClaims, Student{
err = controller.finishAuthorization(authOptionsClaims, dto.Student{
Id: userMeResponse.StudentId,
LastName: userMeResponse.LastName,
FirstName: userMeResponse.FirstName,
Expand All @@ -188,7 +168,7 @@ func (controller *ApiController) completeAuth(c *gin.Context) {
}
}

func (controller *ApiController) successRedirect(c *gin.Context, claims AuthOptionsClaims) {
func (controller *ApiController) successRedirect(c *gin.Context, claims dto.AuthOptionsClaims) {
redirectUri := claims.RedirectUri
if redirectUri == "" {
redirectUri = controller.config.publicUrl + "/close.html"
Expand Down Expand Up @@ -223,7 +203,7 @@ func (controller *ApiController) completeAdminAuth(c *gin.Context) {

err = errors.New("not enough rights")
if adminUserid == authOptionsClaims.KneuUserId {
err = controller.finishAuthorization(authOptionsClaims, Student{
err = controller.finishAuthorization(authOptionsClaims, dto.Student{
Id: uint(studentId),
LastName: "Адмін",
FirstName: "Адмін#" + strconv.FormatUint(studentId, 10),
Expand All @@ -240,7 +220,7 @@ func (controller *ApiController) completeAdminAuth(c *gin.Context) {
controller.errorResponse(c, err.Error())
}

func (controller *ApiController) finishAuthorization(claims AuthOptionsClaims, student Student) error {
func (controller *ApiController) finishAuthorization(claims dto.AuthOptionsClaims, student dto.Student) error {
event := events.UserAuthorizedEvent{
Client: claims.Client,
ClientUserId: claims.ClientUserId,
Expand All @@ -261,15 +241,15 @@ func (controller *ApiController) finishAuthorization(claims AuthOptionsClaims, s
)
}

func (controller *ApiController) buildState(authOptionsClaims AuthOptionsClaims) (state string, err error) {
func (controller *ApiController) buildState(authOptionsClaims dto.AuthOptionsClaims) (state string, err error) {
token := jwt.NewWithClaims(jwt.SigningMethodHS512, &authOptionsClaims)
state, err = token.SignedString(controller.config.jwtSecretKey)
return
}

var jwtParser = jwt.NewParser(jwt.WithValidMethods([]string{"HS512"}))

func (controller *ApiController) parseState(state string) (claims AuthOptionsClaims, err error) {
func (controller *ApiController) parseState(state string) (claims dto.AuthOptionsClaims, err error) {
_, err = jwtParser.ParseWithClaims(
state, &claims,
func(token *jwt.Token) (interface{}, error) {
Expand Down
27 changes: 14 additions & 13 deletions ApiController_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"authorizer/dto"
"bytes"
"context"
"encoding/json"
Expand Down Expand Up @@ -87,13 +88,13 @@ func TestGetAuthUrl(t *testing.T) {

assert.Equal(t, http.StatusOK, w.Code)

response := GetAuthUrlResponse{}
response := dto.GetAuthUrlResponse{}
err := json.NewDecoder(w.Body).Decode(&response)
assert.NoError(t, err)

assert.Equal(t, expectedOauthUrl, response.AuthUrl)

authOptionsClaims := AuthOptionsClaims{}
authOptionsClaims := dto.AuthOptionsClaims{}
_, err = jwtParser.ParseWithClaims(
receivedState, &authOptionsClaims,
func(token *jwt.Token) (interface{}, error) {
Expand Down Expand Up @@ -208,7 +209,7 @@ func TestCompleteAuth(t *testing.T) {

router := (controller).setupRouter()

authOptionsClaims := AuthOptionsClaims{
authOptionsClaims := dto.AuthOptionsClaims{
RegisteredClaims: jwt.RegisteredClaims{
Issuer: "pigeonAuthorizer",
ExpiresAt: jwt.NewNumericDate(time.Now().Add(stateLifetime)),
Expand Down Expand Up @@ -297,7 +298,7 @@ func TestCompleteAuth(t *testing.T) {

router := (controller).setupRouter()

authOptionsClaims := AuthOptionsClaims{
authOptionsClaims := dto.AuthOptionsClaims{
RegisteredClaims: jwt.RegisteredClaims{
Issuer: "pigeonAuthorizer",
ExpiresAt: jwt.NewNumericDate(time.Now().Add(stateLifetime)),
Expand Down Expand Up @@ -344,7 +345,7 @@ func TestCompleteAuth(t *testing.T) {

router := (controller).setupRouter()

authOptionsClaims := AuthOptionsClaims{
authOptionsClaims := dto.AuthOptionsClaims{
RegisteredClaims: jwt.RegisteredClaims{
Issuer: "pigeonAuthorizer",
ExpiresAt: jwt.NewNumericDate(time.Now().Add(stateLifetime)),
Expand Down Expand Up @@ -423,7 +424,7 @@ func TestCompleteAuth(t *testing.T) {

router := (controller).setupRouter()

authOptionsClaims := AuthOptionsClaims{
authOptionsClaims := dto.AuthOptionsClaims{
RegisteredClaims: jwt.RegisteredClaims{
Issuer: "pigeonAuthorizer",
ExpiresAt: jwt.NewNumericDate(time.Now().Add(stateLifetime)),
Expand Down Expand Up @@ -471,7 +472,7 @@ func TestCompleteAuth(t *testing.T) {

router := (controller).setupRouter()

authOptionsClaims := AuthOptionsClaims{
authOptionsClaims := dto.AuthOptionsClaims{
RegisteredClaims: jwt.RegisteredClaims{
Issuer: "pigeonAuthorizer",
ExpiresAt: jwt.NewNumericDate(time.Now().Add(-time.Hour * 6)),
Expand Down Expand Up @@ -558,7 +559,7 @@ func TestCompleteAuth(t *testing.T) {

router := (controller).setupRouter()

authOptionsClaims := AuthOptionsClaims{
authOptionsClaims := dto.AuthOptionsClaims{
RegisteredClaims: jwt.RegisteredClaims{
Issuer: "pigeonAuthorizer",
ExpiresAt: jwt.NewNumericDate(time.Now().Add(stateLifetime)),
Expand Down Expand Up @@ -619,7 +620,7 @@ func TestCompleteAuth(t *testing.T) {

router := (controller).setupRouter()

authOptionsClaims := AuthOptionsClaims{
authOptionsClaims := dto.AuthOptionsClaims{
RegisteredClaims: jwt.RegisteredClaims{
Issuer: "pigeonAuthorizer",
ExpiresAt: jwt.NewNumericDate(time.Now().Add(stateLifetime)),
Expand Down Expand Up @@ -714,7 +715,7 @@ func TestCompleteAuth(t *testing.T) {

router := (controller).setupRouter()

authOptionsClaims := AuthOptionsClaims{
authOptionsClaims := dto.AuthOptionsClaims{
RegisteredClaims: jwt.RegisteredClaims{
Issuer: "pigeonAuthorizer",
ExpiresAt: jwt.NewNumericDate(time.Now().Add(stateLifetime)),
Expand Down Expand Up @@ -763,7 +764,7 @@ func TestCompleteAuth(t *testing.T) {

router := (controller).setupRouter()

authOptionsClaims := AuthOptionsClaims{
authOptionsClaims := dto.AuthOptionsClaims{
RegisteredClaims: jwt.RegisteredClaims{
Issuer: "pigeonAuthorizer",
ExpiresAt: jwt.NewNumericDate(time.Now().Add(stateLifetime)),
Expand Down Expand Up @@ -836,7 +837,7 @@ func TestCompleteAdminAuth(t *testing.T) {

router := (controller).setupRouter()

authOptionsClaims := AuthOptionsClaims{
authOptionsClaims := dto.AuthOptionsClaims{
RegisteredClaims: jwt.RegisteredClaims{
Issuer: "pigeonAuthorizer",
ExpiresAt: jwt.NewNumericDate(time.Now().Add(stateLifetime)),
Expand Down Expand Up @@ -871,7 +872,7 @@ func TestCompleteAdminAuth(t *testing.T) {

router := (controller).setupRouter()

authOptionsClaims := AuthOptionsClaims{
authOptionsClaims := dto.AuthOptionsClaims{
RegisteredClaims: jwt.RegisteredClaims{
Issuer: "pigeonAuthorizer",
ExpiresAt: jwt.NewNumericDate(time.Now().Add(stateLifetime)),
Expand Down
11 changes: 11 additions & 0 deletions dto/AuthOptionsClaims.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package dto

import "github.com/golang-jwt/jwt/v5"

type AuthOptionsClaims struct {
jwt.RegisteredClaims
Client string `form:"client" json:"client" binding:"required"`
ClientUserId string `form:"client_user_id" json:"clientUserId" binding:"required"`
RedirectUri string `form:"redirect_uri" json:"redirectUri,omitempty"`
KneuUserId uint `form:"-" json:"userId,omitempty"`
}
8 changes: 8 additions & 0 deletions dto/GetAuthUrlResponse.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package dto

import "time"

type GetAuthUrlResponse struct {
AuthUrl string `json:"authUrl" binding:"required"`
ExpireAt time.Time `json:"expire" binding:"required"`
}
11 changes: 11 additions & 0 deletions dto/Student.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package dto

import "github.com/kneu-messenger-pigeon/events"

type Student struct {
Id uint
LastName string
FirstName string
MiddleName string
Gender events.Gender
}

0 comments on commit bf1f868

Please sign in to comment.