Skip to content

Commit

Permalink
Add update for when certificate provider signs
Browse files Browse the repository at this point in the history
  • Loading branch information
hawx committed Jan 3, 2024
1 parent acd14fc commit e513d4f
Show file tree
Hide file tree
Showing 21 changed files with 818 additions and 361 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ down: ## Stop application
docker compose down

test: ## Unit tests
go test ./lambda/get/... ./lambda/create/... ./lambda/update/... ./internal/shared/... -race -covermode=atomic -coverprofile=coverage.out
go test ./... -race -covermode=atomic -coverprofile=coverage.out

test-api: URL ?= http://localhost:9000
test-api:
Expand Down
2 changes: 1 addition & 1 deletion docs/openapi/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,6 @@ paths:
httpMethod: "POST"
type: "aws_proxy"
contentHandling: "CONVERT_TO_TEXT"

/health-check:
get:
operationId: healthCheck
Expand Down Expand Up @@ -448,6 +447,7 @@ components:
- DONOR_ADDRESS_UPDATE
- ATTORNEY_ADDRESS_UPDATE
- SCANNING_CORRECTION
- CERTIFICATE_PROVIDER_SIGN
changes:
type: array
items:
Expand Down
69 changes: 69 additions & 0 deletions internal/ddb/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package ddb

import (
"context"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/dynamodb"
"github.com/aws/aws-sdk-go/service/dynamodb/dynamodbattribute"
"github.com/aws/aws-xray-sdk-go/xray"
"github.com/ministryofjustice/opg-data-lpa-store/internal/shared"
)

type Client struct {
ddb *dynamodb.DynamoDB
tableName string
}

func (c *Client) Put(ctx context.Context, data any) error {
item, err := dynamodbattribute.MarshalMap(data)
if err != nil {
return err
}

_, err = c.ddb.PutItemWithContext(ctx, &dynamodb.PutItemInput{
TableName: aws.String(c.tableName),
Item: item,
})

return err
}

func (c *Client) Get(ctx context.Context, uid string) (shared.Lpa, error) {
lpa := shared.Lpa{}

marshalledUid, err := dynamodbattribute.Marshal(uid)
if err != nil {
return lpa, err
}

getItemOutput, err := c.ddb.GetItemWithContext(ctx, &dynamodb.GetItemInput{
TableName: aws.String(c.tableName),
Key: map[string]*dynamodb.AttributeValue{
"uid": marshalledUid,
},
})

if err != nil {
return lpa, err
}

err = dynamodbattribute.UnmarshalMap(getItemOutput.Item, &lpa)

return lpa, err
}

func New(endpoint, tableName string) *Client {
sess := session.Must(session.NewSession())
sess.Config.Endpoint = &endpoint

c := &Client{
ddb: dynamodb.New(sess),
tableName: tableName,
}

xray.AWS(c.ddb.Client)

return c
}
10 changes: 0 additions & 10 deletions internal/shared/client.go

This file was deleted.

70 changes: 0 additions & 70 deletions internal/shared/ddb.go
Original file line number Diff line number Diff line change
@@ -1,71 +1 @@
package shared

import (
"context"
"os"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/dynamodb"
"github.com/aws/aws-sdk-go/service/dynamodb/dynamodbattribute"
"github.com/aws/aws-xray-sdk-go/xray"
)

type DynamoDBClient struct {
ddb *dynamodb.DynamoDB
tableName string
}

func (c DynamoDBClient) Put(ctx context.Context, data Lpa) error {
item, err := dynamodbattribute.MarshalMap(data)
if err != nil {
return err
}

_, err = c.ddb.PutItemWithContext(ctx, &dynamodb.PutItemInput{
TableName: aws.String(c.tableName),
Item: item,
})

return err
}

func (c DynamoDBClient) Get(ctx context.Context, uid string) (Lpa, error) {
lpa := Lpa{}

marshalledUid, err := dynamodbattribute.Marshal(uid)
if err != nil {
return lpa, err
}

getItemOutput, err := c.ddb.GetItemWithContext(ctx, &dynamodb.GetItemInput{
TableName: aws.String(c.tableName),
Key: map[string]*dynamodb.AttributeValue{
"uid": marshalledUid,
},
})

if err != nil {
return lpa, err
}

err = dynamodbattribute.UnmarshalMap(getItemOutput.Item, &lpa)

return lpa, err
}

func NewDynamoDB(tableName string) DynamoDBClient {
sess := session.Must(session.NewSession())

endpoint := os.Getenv("AWS_DYNAMODB_ENDPOINT")
sess.Config.Endpoint = &endpoint

c := DynamoDBClient{
ddb: dynamodb.New(sess),
tableName: tableName,
}

xray.AWS(c.ddb.Client)

return c
}
40 changes: 20 additions & 20 deletions internal/shared/jwt.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,25 +88,6 @@ func NewJWTVerifier() JWTVerifier {
}
}

// tokenStr is the JWT token, minus any "Bearer: " prefix
func (v JWTVerifier) VerifyToken(tokenStr string) error {
lsc := lpaStoreClaims{}

parsedToken, err := jwt.ParseWithClaims(tokenStr, &lsc, func(token *jwt.Token) (interface{}, error) {
return v.secretKey, nil
})

if err != nil {
return err
}

if !parsedToken.Valid {
return fmt.Errorf("Invalid JWT")
}

return nil
}

var bearerRegexp = regexp.MustCompile("^Bearer[ ]+")

// verify JWT from event header
Expand All @@ -119,9 +100,28 @@ func (v JWTVerifier) VerifyHeader(event events.APIGatewayProxyRequest) bool {
}

tokenStr := bearerRegexp.ReplaceAllString(jwtHeaders[0], "")
if v.VerifyToken(tokenStr) != nil {
if v.verifyToken(tokenStr) != nil {
return false
}

return true
}

// tokenStr is the JWT token, minus any "Bearer: " prefix
func (v JWTVerifier) verifyToken(tokenStr string) error {
lsc := lpaStoreClaims{}

parsedToken, err := jwt.ParseWithClaims(tokenStr, &lsc, func(token *jwt.Token) (interface{}, error) {
return v.secretKey, nil
})

if err != nil {
return err
}

if !parsedToken.Valid {
return fmt.Errorf("Invalid JWT")
}

return nil
}
16 changes: 8 additions & 8 deletions internal/shared/jwt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func createToken(claims jwt.MapClaims) string {
}

func TestVerifyEmptyJwt(t *testing.T) {
err := verifier.VerifyToken("")
err := verifier.verifyToken("")
assert.NotNil(t, err)
}

Expand All @@ -38,7 +38,7 @@ func TestVerifyExpInPast(t *testing.T) {
"sub": "M-3467-89QW-ERTY",
})

err := verifier.VerifyToken(token)
err := verifier.verifyToken(token)

assert.NotNil(t, err)
if err != nil {
Expand All @@ -54,7 +54,7 @@ func TestVerifyIatInFuture(t *testing.T) {
"sub": "[email protected]",
})

err := verifier.VerifyToken(token)
err := verifier.verifyToken(token)

assert.NotNil(t, err)
if err != nil {
Expand All @@ -70,7 +70,7 @@ func TestVerifyIssuer(t *testing.T) {
"sub": "[email protected]",
})

err := verifier.VerifyToken(token)
err := verifier.verifyToken(token)

assert.NotNil(t, err)
if err != nil {
Expand All @@ -86,7 +86,7 @@ func TestVerifyBadEmailForSiriusIssuer(t *testing.T) {
"sub": "",
})

err := verifier.VerifyToken(token)
err := verifier.verifyToken(token)

assert.NotNil(t, err)
if err != nil {
Expand All @@ -102,7 +102,7 @@ func TestVerifyBadUIDForMRLPAIssuer(t *testing.T) {
"sub": "",
})

err := verifier.VerifyToken(token)
err := verifier.verifyToken(token)

assert.NotNil(t, err)
if err != nil {
Expand All @@ -118,7 +118,7 @@ func TestVerifyGoodJwt(t *testing.T) {
"sub": "[email protected]",
})

err := verifier.VerifyToken(token)
err := verifier.verifyToken(token)
assert.Nil(t, err)
}

Expand All @@ -134,7 +134,7 @@ func TestNewJWTVerifier(t *testing.T) {
newVerifier := NewJWTVerifier()
os.Unsetenv("JWT_SECRET_KEY")

err := newVerifier.VerifyToken(token)
err := newVerifier.verifyToken(token)
assert.Nil(t, err)
}

Expand Down
12 changes: 12 additions & 0 deletions internal/shared/lang.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package shared

type Lang string

var (
LangCy = Lang("cy")
LangEn = Lang("en")
)

func (l Lang) IsValid() bool {
return l == LangCy || l == LangEn
}
4 changes: 4 additions & 0 deletions internal/shared/person.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ type Address struct {
Country string `json:"country"`
}

func (a Address) IsSet() bool {
return a.Line1 != "" || a.Line2 != "" || a.Line3 != "" || a.Town != "" || a.Postcode != "" || a.Country != ""
}

type Person struct {
FirstNames string `json:"firstNames"`
LastName string `json:"lastName"`
Expand Down
8 changes: 5 additions & 3 deletions internal/shared/update.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package shared

import "encoding/json"

type Change struct {
Key string `json:"key"`
Old interface{} `json:"old"`
New interface{} `json:"new"`
Key string `json:"key"`
Old json.RawMessage `json:"old"`
New json.RawMessage `json:"new"`
}

type Update struct {
Expand Down
Loading

0 comments on commit e513d4f

Please sign in to comment.