-
Notifications
You must be signed in to change notification settings - Fork 4
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 #33 from NorskHelsenett/add-context-services
Added context services
- Loading branch information
Showing
3 changed files
with
118 additions
and
0 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
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 | ||
} |
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,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 | ||
} |
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,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 | ||
} |