Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 19 additions & 3 deletions jwt.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ import (

// SdkToken represents the response for a request for a JWT token
type SdkToken struct {
ApplicantID string `json:"applicant_id,omitempty"`
Referrer string `json:"referrer,omitempty"`
Token string `json:"token,omitempty"`
ApplicantID string `json:"applicant_id,omitempty"`
Referrer string `json:"referrer,omitempty"`
Token string `json:"token,omitempty"`
ApplicationID string `json:"application_id,omitempty"`
}

// NewSdkToken returns a JWT token to used by the Javascript SDK
Expand All @@ -19,6 +20,21 @@ func (c *Client) NewSdkToken(ctx context.Context, id, referrer string) (*SdkToke
ApplicantID: id,
Referrer: referrer,
}
return c.sdkTokenRequest(ctx, t)
}

// NewSDKTokenForMobileApp returns a JWT token to used by Onfido's Mobile Application components.
// These require an Application ID rather than a Referrer URL.
// See https://github.com/onfido/onfido-ios-sdk#31-sdk-tokens
func (c *Client) NewSDKTokenForMobileApp(ctx context.Context, id, applicationID string) (*SdkToken, error) {
t := &SdkToken{
ApplicantID: id,
ApplicationID: applicationID,
}
return c.sdkTokenRequest(ctx, t)
}

func (c *Client) sdkTokenRequest(ctx context.Context, t *SdkToken) (*SdkToken, error) {
jsonStr, err := json.Marshal(t)
if err != nil {
return nil, err
Expand Down
84 changes: 54 additions & 30 deletions jwt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,40 +34,64 @@ func TestNewSdkToken_NonOKResponse(t *testing.T) {
}

func TestNewSdkToken_ApplicantsRetrieved(t *testing.T) {
expected := onfido.SdkToken{
ApplicantID: "klj25h2jk5j4k5jk35",
Referrer: "https://*.uw-labs.co.uk/documentation/*",
Token: "423423m4n234czxKJKDLF",
}
expectedJSON, err := json.Marshal(expected)
if err != nil {
t.Fatal(err)
testCases := []onfido.SdkToken{
{
ApplicantID: "klj25h2jk5j4k5jk35",
Referrer: "https://*.uw-labs.co.uk/documentation/*",
Token: "423423m4n234czxKJKDLF",
},
{
ApplicantID: "maf92h1qa5j4g3si34",
ApplicationID: "com.ios.application",
Token: "534534m4n234czxQIKKLF",
},
}

m := mux.NewRouter()
m.HandleFunc("/sdk_token", func(w http.ResponseWriter, r *http.Request) {
var tk onfido.SdkToken
assert.NoError(t, json.NewDecoder(r.Body).Decode(&tk))
assert.Equal(t, expected.ApplicantID, tk.ApplicantID)
assert.Equal(t, expected.Referrer, tk.Referrer)
for i := range testCases {
expected := testCases[i] // pinning demanded by golint!
expectedJSON, err := json.Marshal(expected)
if err != nil {
t.Fatal(err)
}

w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
_, wErr := w.Write(expectedJSON)
assert.NoError(t, wErr)
}).Methods("POST")
srv := httptest.NewServer(m)
defer srv.Close()
m := mux.NewRouter()
m.HandleFunc("/sdk_token", func(w http.ResponseWriter, r *http.Request) {
var tk onfido.SdkToken
assert.NoError(t, json.NewDecoder(r.Body).Decode(&tk))
assert.Equal(t, expected.ApplicantID, tk.ApplicantID)
assert.Equal(t, expected.Referrer, tk.Referrer)
assert.Equal(t, expected.ApplicationID, tk.ApplicationID)

client := onfido.NewClient("123")
client.Endpoint = srv.URL
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
_, wErr := w.Write(expectedJSON)
assert.NoError(t, wErr)
}).Methods("POST")
srv := httptest.NewServer(m)
defer srv.Close()

token, err := client.NewSdkToken(context.Background(), expected.ApplicantID, expected.Referrer)
if err != nil {
t.Fatal(err)
}
client := onfido.NewClient("123")
client.Endpoint = srv.URL

assert.Equal(t, expected.ApplicantID, token.ApplicantID)
assert.Equal(t, expected.Referrer, token.Referrer)
assert.Equal(t, expected.Token, token.Token)
var sdkToken *onfido.SdkToken
switch {
case expected.Referrer != "":
sdkToken, err = client.NewSdkToken(context.Background(), expected.ApplicantID, expected.Referrer)
if err != nil {
t.Fatal(err)
}
case expected.ApplicationID != "":
sdkToken, err = client.NewSDKTokenForMobileApp(context.Background(), expected.ApplicantID, expected.ApplicationID)
if err != nil {
t.Fatal(err)
}
default:
t.Fatal("neither a Referrer or Application ID was specified in the Token request")
}

assert.Equal(t, expected.ApplicantID, sdkToken.ApplicantID)
assert.Equal(t, expected.Referrer, sdkToken.Referrer)
assert.Equal(t, expected.ApplicationID, sdkToken.ApplicationID)
assert.Equal(t, expected.Token, sdkToken.Token)
}
}
2 changes: 1 addition & 1 deletion live_photos.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func (i *LivePhotoIter) LivePhoto() *LivePhoto {
return i.Current().(*LivePhoto)
}

// ListPhotos retrieves the list of photos for the provided applicant.
// ListLivePhotos retrieves the list of photos for the provided applicant.
// see https://documentation.onfido.com/?shell#live-photos
func (c *Client) ListLivePhotos(applicantID string) *LivePhotoIter {
return &LivePhotoIter{&iter{
Expand Down