Skip to content

Commit

Permalink
#312 - Renamed the struct to be in line with interface , added initia…
Browse files Browse the repository at this point in the history
…lization in components
  • Loading branch information
anandrgitnirman authored and anandrgitnirman committed May 21, 2020
1 parent 512082f commit 2e52f98
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 7 deletions.
12 changes: 12 additions & 0 deletions snetd/cmd/components.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/singnet/snet-daemon/configuration_service"
"github.com/singnet/snet-daemon/metrics"
"github.com/singnet/snet-daemon/pricing"
"github.com/singnet/snet-daemon/token"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
Expand Down Expand Up @@ -50,6 +51,7 @@ type Components struct {
freeCallUserService escrow.FreeCallUserService
freeCallUserStorage *escrow.FreeCallUserStorage
freeCallLockerStorage *escrow.PrefixedAtomicStorage
tokenService token.Service
}

func InitComponents(cmd *cobra.Command) (components *Components) {
Expand Down Expand Up @@ -505,3 +507,13 @@ func (components *Components) ConfigurationService() *configuration_service.Conf

return components.configurationService
}

func (components *Components) TokenService() token.Service {
if components.tokenService != nil {
return components.tokenService
}

components.tokenService = token.NewJWTTokenService(*components.OrganizationMetaData())

return components.tokenService
}
18 changes: 14 additions & 4 deletions token/jwttoken.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,26 @@ package token
import (
"fmt"
"github.com/dgrijalva/jwt-go"
"github.com/singnet/snet-daemon/blockchain"
"github.com/singnet/snet-daemon/config"
"strings"
"time"
)

type customJWTokenClaimsImpl struct {
type customJWTokenServiceImpl struct {
getGroupId func() string
}

func (service customJWTokenClaimsImpl) CreateToken(payLoad PayLoad) (CustomToken, error) {
//This will be used in components as a service to Create and Validate tokens
func NewJWTTokenService(data blockchain.OrganizationMetaData) Service {
return &customJWTokenServiceImpl{
getGroupId: func() string {
return data.GetGroupIdString()
},
}
}

func (service customJWTokenServiceImpl) CreateToken(payLoad PayLoad) (CustomToken, error) {
atClaims := jwt.MapClaims{}
atClaims["payload"] = fmt.Sprintf("%v", payLoad)
atClaims["orgId"] = config.GetString(config.OrganizationId)
Expand All @@ -24,7 +34,7 @@ func (service customJWTokenClaimsImpl) CreateToken(payLoad PayLoad) (CustomToken
return jwtToken.SignedString([]byte(config.GetString(config.TokenSecretKey)))
}

func (service customJWTokenClaimsImpl) VerifyToken(receivedToken CustomToken, payLoad PayLoad) (err error) {
func (service customJWTokenServiceImpl) VerifyToken(receivedToken CustomToken, payLoad PayLoad) (err error) {
tokenString := fmt.Sprintf("%v", receivedToken)
token, err := jwt.Parse(tokenString, func(token *jwt.Token) (interface{}, error) {
if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok {
Expand All @@ -44,7 +54,7 @@ func (service customJWTokenClaimsImpl) VerifyToken(receivedToken CustomToken, pa
return nil
}

func (service customJWTokenClaimsImpl) checkJwtTokenClaims(claims jwt.MapClaims, payload PayLoad) (err error) {
func (service customJWTokenServiceImpl) checkJwtTokenClaims(claims jwt.MapClaims, payload PayLoad) (err error) {
if strings.Compare(fmt.Sprintf("%v", claims["payload"]), fmt.Sprintf("%v", payload)) != 0 {
return fmt.Errorf("payload %v used to generate the Token doesnt match expected values", claims["payload"])
}
Expand Down
6 changes: 3 additions & 3 deletions token/jwttoken_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
)

func Test_customJWTokenClaimsImpl_CreateToken(t *testing.T) {
tokenImpl := &customJWTokenClaimsImpl{
tokenImpl := &customJWTokenServiceImpl{
getGroupId: func() string {
return "GroupID"
},
Expand All @@ -28,7 +28,7 @@ func Test_customJWTokenClaimsImpl_CreateToken(t *testing.T) {
}

func Test_customJWTokenClaimsImpl_checkJwtTokenClaims(t *testing.T) {
tokenImpl := &customJWTokenClaimsImpl{
tokenImpl := &customJWTokenServiceImpl{
getGroupId: func() string {
return "GroupID"
},
Expand All @@ -41,7 +41,7 @@ func Test_customJWTokenClaimsImpl_checkJwtTokenClaims(t *testing.T) {
err = tokenImpl.VerifyToken(token, "any struct")
assert.Equal(t, "organization ExampleOrganizationId is not associated with this Daemon", err.Error())
config.Vip().Set(config.OrganizationId, "ExampleOrganizationId")
tokenImpl2 := &customJWTokenClaimsImpl{
tokenImpl2 := &customJWTokenServiceImpl{
getGroupId: func() string {
return "GroupID2"
},
Expand Down

0 comments on commit 2e52f98

Please sign in to comment.