-
Notifications
You must be signed in to change notification settings - Fork 0
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 #32 from rarimo/feature/join-program
Feature: join rewards program
- Loading branch information
Showing
16 changed files
with
344 additions
and
64 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
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,15 @@ | ||
allOf: | ||
- $ref: '#/components/schemas/JoinProgramKey' | ||
- type: object | ||
x-go-is-request: true | ||
required: | ||
- attributes | ||
properties: | ||
attributes: | ||
type: object | ||
required: | ||
- country | ||
properties: | ||
country: | ||
type: string | ||
example: "5589842" |
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,13 @@ | ||
type: object | ||
required: | ||
- id | ||
- type | ||
properties: | ||
id: | ||
type: string | ||
description: Nullifier of the points owner | ||
example: "0x123...abc" | ||
pattern: '^0x[0-9a-fA-F]{64}$' | ||
type: | ||
type: string | ||
enum: [ join_program ] |
49 changes: 49 additions & 0 deletions
49
...pec/paths/integrations@rarime-points-svc@v1@public@balances@{nullifier}@join_program.yaml
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,49 @@ | ||
post: | ||
tags: | ||
- Points balance | ||
summary: Join rewards program | ||
description: Join rewards program | ||
operationId: joinRewardsProgram | ||
parameters: | ||
- $ref: '#/components/parameters/pathNullifier' | ||
requestBody: | ||
required: true | ||
content: | ||
application/vnd.api+json: | ||
schema: | ||
type: object | ||
required: | ||
- data | ||
properties: | ||
data: | ||
$ref: '#/components/schemas/JoinProgram' | ||
responses: | ||
200: | ||
description: Success | ||
content: | ||
application/vnd.api+json: | ||
schema: | ||
type: object | ||
required: | ||
- data | ||
properties: | ||
data: | ||
$ref: '#/components/schemas/PassportEventState' | ||
400: | ||
$ref: '#/components/responses/invalidParameter' | ||
401: | ||
$ref: '#/components/responses/invalidAuth' | ||
404: | ||
description: Balance not exists. | ||
content: | ||
application/vnd.api+json: | ||
schema: | ||
$ref: '#/components/schemas/Errors' | ||
429: | ||
description: Passport already verified or event absent for user. | ||
content: | ||
application/vnd.api+json: | ||
schema: | ||
$ref: '#/components/schemas/Errors' | ||
500: | ||
$ref: '#/components/responses/internalError' |
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,5 @@ | ||
-- +migrate Up | ||
ALTER TABLE balances ADD COLUMN is_passport_proven boolean NOT NULL DEFAULT FALSE; | ||
|
||
-- +migrate Down | ||
ALTER TABLE balances DROP COLUMN is_passport_proven; |
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
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
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,77 @@ | ||
package handlers | ||
|
||
import ( | ||
"crypto/hmac" | ||
"crypto/sha256" | ||
"encoding/hex" | ||
"fmt" | ||
"net/http" | ||
|
||
"github.com/rarimo/rarime-points-svc/internal/data" | ||
"github.com/rarimo/rarime-points-svc/internal/data/evtypes" | ||
"github.com/rarimo/rarime-points-svc/internal/service/requests" | ||
"gitlab.com/distributed_lab/ape" | ||
"gitlab.com/distributed_lab/ape/problems" | ||
) | ||
|
||
func JoinProgram(w http.ResponseWriter, r *http.Request) { | ||
req, err := requests.NewJoinProgram(r) | ||
if err != nil { | ||
Log(r).WithError(err).Debug("Bad request") | ||
ape.RenderErr(w, problems.BadRequest(err)...) | ||
return | ||
} | ||
|
||
gotSig := r.Header.Get("Signature") | ||
wantSig := calculateCountrySignature(CountriesConfig(r).VerificationKey, req.Data.ID, req.Data.Attributes.Country) | ||
if gotSig != wantSig { | ||
Log(r).Warnf("Unauthorized access: HMAC signature mismatch: got %s, want %s", gotSig, wantSig) | ||
ape.RenderErr(w, problems.Forbidden()) | ||
return | ||
} | ||
|
||
balance, errs := getAndVerifyBalanceEligibility(r, req.Data.ID, nil) | ||
if len(errs) > 0 { | ||
ape.RenderErr(w, errs...) | ||
return | ||
} | ||
|
||
if balance.Country != nil { | ||
Log(r).Debugf("Balance %s already joined rewards program", balance.Nullifier) | ||
ape.RenderErr(w, problems.TooManyRequests()) | ||
return | ||
} | ||
|
||
err = EventsQ(r).Transaction(func() error { | ||
return doPassportScanUpdates(r, *balance, req.Data.Attributes.Country, false) | ||
}) | ||
if err != nil { | ||
Log(r).WithError(err).Error("Failed to execute transaction") | ||
ape.RenderErr(w, problems.InternalError()) | ||
return | ||
} | ||
|
||
event, err := EventsQ(r).FilterByNullifier(balance.Nullifier). | ||
FilterByType(evtypes.TypePassportScan). | ||
FilterByStatus(data.EventClaimed).Get() | ||
if err != nil { | ||
Log(r).WithError(err).Error("Failed to get claimed event") | ||
ape.RenderErr(w, problems.InternalError()) | ||
return | ||
} | ||
|
||
ape.Render(w, newPassportEventStateResponse(req.Data.ID, event)) | ||
} | ||
|
||
func calculateCountrySignature(key []byte, nullifier, country string) string { | ||
bNull, err := hex.DecodeString(nullifier[2:]) | ||
if err != nil { | ||
panic(fmt.Errorf("nullifier was not properly validated as hex: %w", err)) | ||
} | ||
|
||
h := hmac.New(sha256.New, key) | ||
msg := append(bNull, []byte(country)...) | ||
h.Write(msg) | ||
|
||
return hex.EncodeToString(h.Sum(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
Oops, something went wrong.