Skip to content

Commit

Permalink
feat: add request token support to all methods
Browse files Browse the repository at this point in the history
  • Loading branch information
figueredo committed Jul 25, 2024
1 parent 9a63714 commit c90faf4
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 20 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ This method registers a new **web** payment for the given installation and accou

```go
assessment, err := client.RegisterPayment(&incognia.Payment{
SessionToken: "session-token",
RequestToken: "request-token",
AccountID: "account-id",
ExternalID: "external-id",
PolicyID: "policy-id",
Expand All @@ -228,11 +228,11 @@ assessment, err := client.RegisterLogin(&incognia.Login{
})
```

This method registers a new **web** login for the given account and session-token, returning a `TransactionAssessment`, containing the risk assessment and supporting evidence.
This method registers a new **web** login for the given account and request-token, returning a `TransactionAssessment`, containing the risk assessment and supporting evidence.

```go
assessment, err := client.RegisterLogin(&incognia.Login{
SessionToken: "session-token",
RequestToken: "request-token",
AccountID: "account-id",
...
})
Expand Down Expand Up @@ -284,9 +284,9 @@ assessment, err := client.RegisterPayment(&incognia.Payment{
This method registers a feedback event for the given identifiers (represented in `FeedbackIdentifiers`) related to a signup, login or payment.
```go
occurred_at := time.Parse(time.RFC3339, "2024-07-22T15:20:00Z")
occurredAt, err := time.Parse(time.RFC3339, "2024-07-22T15:20:00Z")
feedbackEvent := incognia.AccountTakeover
err := client.RegisterFeedback(feedbackEvent, &occurred_at, &incognia.FeedbackIdentifiers{
err := client.RegisterFeedback(feedbackEvent, &occurredAt, &incognia.FeedbackIdentifiers{
InstallationID: "some-installation-id",
AccountID: "some-account-id",
})
Expand Down
21 changes: 15 additions & 6 deletions incognia.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ var (
ErrMissingSignup = errors.New("missing signup parameters")
ErrMissingInstallationID = errors.New("missing installation id")
ErrMissingInstallationIDOrSessionToken = errors.New("missing installation id or session token")
ErrMissingIdentifier = errors.New("missing installation id, request token or session token")
ErrMissingAccountID = errors.New("missing account id")
ErrMissingSignupID = errors.New("missing signup id")
ErrMissingTimestamp = errors.New("missing timestamp")
Expand Down Expand Up @@ -57,6 +58,7 @@ type IncogniaClientConfig struct {
type Payment struct {
InstallationID *string
SessionToken *string
RequestToken string
AccountID string
ExternalID string
PolicyID string
Expand All @@ -69,6 +71,7 @@ type Payment struct {
type Login struct {
InstallationID *string
SessionToken *string
RequestToken string
AccountID string
ExternalID string
PolicyID string
Expand All @@ -95,6 +98,8 @@ type Address struct {

type Signup struct {
InstallationID string
RequestToken string
SessionToken string
Address *Address
AccountID string
PolicyID string
Expand Down Expand Up @@ -201,12 +206,14 @@ func (c *Client) registerSignup(params *Signup) (ret *SignupAssessment, err erro
if params == nil {
return nil, ErrMissingSignup
}
if params.InstallationID == "" {
return nil, ErrMissingInstallationID
if params.InstallationID == "" && params.RequestToken == "" && params.SessionToken == "" {
return nil, ErrMissingIdentifier
}

requestBody := postAssessmentRequestBody{
InstallationID: params.InstallationID,
RequestToken: params.RequestToken,
SessionToken: params.SessionToken,
AccountID: params.AccountID,
PolicyID: params.PolicyID,
}
Expand Down Expand Up @@ -306,8 +313,8 @@ func (c *Client) registerPayment(payment *Payment) (ret *TransactionAssessment,
return nil, ErrMissingPayment
}

if payment.InstallationID == nil && payment.SessionToken == nil {
return nil, ErrMissingInstallationIDOrSessionToken
if payment.InstallationID == nil && payment.SessionToken == nil && payment.RequestToken == "" {
return nil, ErrMissingIdentifier
}

if payment.AccountID == "" {
Expand All @@ -316,6 +323,7 @@ func (c *Client) registerPayment(payment *Payment) (ret *TransactionAssessment,

requestBody, err := json.Marshal(postTransactionRequestBody{
InstallationID: payment.InstallationID,
RequestToken: payment.RequestToken,
SessionToken: payment.SessionToken,
Type: paymentType,
AccountID: payment.AccountID,
Expand Down Expand Up @@ -366,8 +374,8 @@ func (c *Client) registerLogin(login *Login) (*TransactionAssessment, error) {
return nil, ErrMissingLogin
}

if login.InstallationID == nil && login.SessionToken == nil {
return nil, ErrMissingInstallationIDOrSessionToken
if login.InstallationID == nil && login.SessionToken == nil && login.RequestToken == "" {
return nil, ErrMissingIdentifier
}

if login.AccountID == "" {
Expand All @@ -382,6 +390,7 @@ func (c *Client) registerLogin(login *Login) (*TransactionAssessment, error) {
ExternalID: login.ExternalID,
PaymentMethodIdentifier: login.PaymentMethodIdentifier,
SessionToken: login.SessionToken,
RequestToken: login.RequestToken,
})
if err != nil {
return nil, err
Expand Down
19 changes: 11 additions & 8 deletions incognia_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ var (
now = time.Now()
nowMinusSeconds = now.Add(-1 * time.Second)
installationId = "installation-id"
sessionToken = "session-token"
requestToken = "request-token"
shouldEval bool = true
shouldNotEval bool = false
emptyQueryString map[string][]string = nil
Expand Down Expand Up @@ -79,6 +79,7 @@ var (

postSignupRequestBodyWithAllParamsFixture = &postAssessmentRequestBody{
InstallationID: installationId,
RequestToken: requestToken,
AddressLine: "address line",
StructuredAddress: &StructuredAddress{
Locale: "locale",
Expand Down Expand Up @@ -218,7 +219,7 @@ var (
},
}
postPaymentWebRequestBodyFixture = &postTransactionRequestBody{
SessionToken: &sessionToken,
RequestToken: requestToken,
AccountID: "account-id",
ExternalID: "external-id",
PolicyID: "policy-id",
Expand Down Expand Up @@ -314,7 +315,7 @@ var (
},
}
paymentWebFixture = &Payment{
SessionToken: &sessionToken,
RequestToken: requestToken,
AccountID: "account-id",
ExternalID: "external-id",
PolicyID: "policy-id",
Expand Down Expand Up @@ -410,7 +411,7 @@ var (
ExternalID: "external-id",
PolicyID: "policy-id",
PaymentMethodIdentifier: "payment-method-identifier",
SessionToken: &sessionToken,
RequestToken: requestToken,
}
postLoginRequestBodyFixture = &postTransactionRequestBody{
InstallationID: &installationId,
Expand All @@ -426,7 +427,7 @@ var (
PolicyID: "policy-id",
PaymentMethodIdentifier: "payment-method-identifier",
Type: loginType,
SessionToken: &sessionToken,
RequestToken: requestToken,
}
)

Expand Down Expand Up @@ -573,6 +574,8 @@ func (suite *IncogniaTestSuite) TestSuccessRegisterSignupWithParams() {

response, err := suite.client.RegisterSignupWithParams(&Signup{
InstallationID: postSignupRequestBodyWithAllParamsFixture.InstallationID,
RequestToken: postSignupRequestBodyWithAllParamsFixture.RequestToken,
SessionToken: postSignupRequestBodyWithAllParamsFixture.SessionToken,
Address: addressFixture,
AccountID: postSignupRequestBodyWithAllParamsFixture.AccountID,
PolicyID: postSignupRequestBodyWithAllParamsFixture.PolicyID,
Expand Down Expand Up @@ -617,7 +620,7 @@ func (suite *IncogniaTestSuite) TestSuccessRegisterSignupAfterTokenExpiration()

func (suite *IncogniaTestSuite) TestRegisterSignupEmptyInstallationId() {
response, err := suite.client.RegisterSignup("", &Address{})
suite.EqualError(err, ErrMissingInstallationID.Error())
suite.EqualError(err, ErrMissingIdentifier.Error())
suite.Nil(response)
}

Expand Down Expand Up @@ -794,7 +797,7 @@ func (suite *IncogniaTestSuite) TestRegisterPaymentNilPayment() {

func (suite *IncogniaTestSuite) TestRegisterPaymentEmptyInstallationId() {
response, err := suite.client.RegisterPayment(&Payment{AccountID: "some-account-id"})
suite.EqualError(err, ErrMissingInstallationIDOrSessionToken.Error())
suite.EqualError(err, ErrMissingIdentifier.Error())
suite.Nil(response)
}

Expand Down Expand Up @@ -903,7 +906,7 @@ func (suite *IncogniaTestSuite) TestRegisterLoginNilLogin() {

func (suite *IncogniaTestSuite) TestRegisterLoginNullInstallationIdAndSessionToken() {
response, err := suite.client.RegisterLogin(&Login{AccountID: "some-account-id"})
suite.EqualError(err, ErrMissingInstallationIDOrSessionToken.Error())
suite.EqualError(err, ErrMissingIdentifier.Error())
suite.Nil(response)
}

Expand Down
5 changes: 4 additions & 1 deletion request_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ type StructuredAddress struct {
}

type postAssessmentRequestBody struct {
InstallationID string `json:"installation_id"`
InstallationID string `json:"installation_id,omitempty"`
RequestToken string `json:"request_token,omitempty"`
SessionToken string `json:"session_token,omitempty"`
AddressLine string `json:"address_line,omitempty"`
StructuredAddress *StructuredAddress `json:"structured_address,omitempty"`
Coordinates *Coordinates `json:"address_coordinates,omitempty"`
Expand Down Expand Up @@ -136,4 +138,5 @@ type postTransactionRequestBody struct {
PaymentValue *PaymentValue `json:"payment_value,omitempty"`
PaymentMethods []*PaymentMethod `json:"payment_methods,omitempty"`
SessionToken *string `json:"session_token,omitempty"`
RequestToken string `json:"request_token,omitempty"`
}

0 comments on commit c90faf4

Please sign in to comment.