Skip to content

Commit

Permalink
Merge pull request #33 from NorskHelsenett/add-context-services
Browse files Browse the repository at this point in the history
Added context services
  • Loading branch information
havardelnan authored Feb 21, 2024
2 parents bd5bbd5 + cba411a commit 514c4ef
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 0 deletions.
66 changes: 66 additions & 0 deletions pkg/context/gincontext/gin.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// Package contextservices The package provides functions to get and work with ror contexts
package gincontext

import (
"context"
"errors"
"net/http"
"time"

identitymodels "github.com/NorskHelsenett/ror/pkg/models/identity"

"github.com/NorskHelsenett/ror/pkg/apicontracts"

"github.com/NorskHelsenett/ror/pkg/rlog"

"github.com/gin-gonic/gin"
)

// GetRorContextFromGinContext Function creates ror context from gin context, identity is added to the context
func GetRorContextFromGinContext(c *gin.Context) (context.Context, context.CancelFunc) {
ctx, cancel := context.WithTimeout(c.Request.Context(), 10*time.Second)
identity, err := getIdentityFromGinContext(c)
if err != nil {
rlog.Error("could not get user from gin context: %v", err)
c.JSON(http.StatusUnauthorized, apicontracts.Error{
Status: http.StatusUnauthorized,
Message: "Could not fetch user",
})
return nil, cancel
}
ctx = context.WithValue(ctx, identitymodels.ContexIdentity, *identity)
return ctx, cancel
}

// GetUserFromGinContext Function extracts the user from the gin context
//
// !!! Should only be used in audit middleware !!!
func GetUserFromGinContext(c *gin.Context) (*identitymodels.User, error) {
userObject, exists := c.Get("user")
if !exists {
return nil, errors.New("user not set in gin context")
}

if userObject == nil {
return nil, errors.New("user object is nil")
}

user := userObject.(identitymodels.User)

return &user, nil
}

// Function extracts the identity from gin context
func getIdentityFromGinContext(c *gin.Context) (*identitymodels.Identity, error) {
identityObj, ok := c.Get("identity")
if !ok {
return nil, errors.New("identity not set in gin context")
}

if identityObj == nil {
return nil, errors.New("identity object is nil")
}

identity := identityObj.(identitymodels.Identity)
return &identity, nil
}
31 changes: 31 additions & 0 deletions pkg/context/mscontext/ms.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// The package provides ror-context for services
package mscontext

import (
"context"
"time"

identitymodels "github.com/NorskHelsenett/ror/pkg/models/identity"
)

// Function provides a ror context for a given servicename, the context must be used to authenticate against existing services
func GetRorContextFromServiceContext(c *context.Context, servicename string) (context.Context, context.CancelFunc) {
ctx, cancel := context.WithTimeout(*c, 10*time.Second)
identity := identitymodels.Identity{
Type: identitymodels.IdentityTypeService,
ServiceIdentity: &identitymodels.ServiceIdentity{Id: servicename},
}
ctx = context.WithValue(ctx, identitymodels.ContexIdentity, identity)
return ctx, cancel
}

// GetRorContextFromServiceContextWithoutCancel Function provides a ror context for a given servicename, the context must be used to authenticate against existing services.
// The functionality is the same except that it forces a cancel on us.
func GetRorContextFromServiceContextWithoutCancel(c context.Context, servicename string) context.Context {
identity := identitymodels.Identity{
Type: identitymodels.IdentityTypeService,
ServiceIdentity: &identitymodels.ServiceIdentity{Id: servicename},
}
ctx := context.WithValue(c, identitymodels.ContexIdentity, identity)
return ctx
}
21 changes: 21 additions & 0 deletions pkg/context/rorcontext/contextservice.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Package contextservices The package provides functions to get and work with ror contexts
package rorcontext

import (
"context"
"fmt"

identitymodels "github.com/NorskHelsenett/ror/pkg/models/identity"

"github.com/NorskHelsenett/ror/pkg/rlog"
)

// GetIdentityFromRorContext Function returns the identity from the ror context.
func GetIdentityFromRorContext(ctx context.Context) identitymodels.Identity {
identity, ok := ctx.Value(identitymodels.ContexIdentity).(identitymodels.Identity)
if !ok {
rlog.Error("failed to get identity from RorContext", fmt.Errorf("error getting identity from context"))
panic("Faild to get identity")
}
return identity
}

0 comments on commit 514c4ef

Please sign in to comment.