-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement verify and start testing it
- Loading branch information
1 parent
14b8a5f
commit 3946039
Showing
7 changed files
with
164 additions
and
13 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
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,67 @@ | ||
package endpoints | ||
|
||
import ( | ||
"fmt" | ||
"io/ioutil" | ||
"net/http" | ||
"net/url" | ||
|
||
"github.com/kwoodhouse93/gotrue-go/types" | ||
) | ||
|
||
const verifyPath = "/verify" | ||
|
||
// Get /verify | ||
// | ||
// Verify a registration or a password recovery. Type can be signup or recovery | ||
// or magiclink or invite and the token is a token returned from either /signup | ||
// or /recover or /magiclink. | ||
func (c *Client) Verify(req types.VerifyRequest) (*types.VerifyResponse, error) { | ||
r, err := c.newRequest(verifyPath, http.MethodGet, nil) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
q := r.URL.Query() | ||
q.Add("type", string(req.Type)) | ||
q.Add("token", req.Token) | ||
q.Add("redirect_to", req.RedirectTo) | ||
r.URL.RawQuery = q.Encode() | ||
|
||
// Set up a client that will not follow the redirect. | ||
noRedirClient := noRedirClient(c.client) | ||
|
||
resp, err := noRedirClient.Do(r) | ||
if err != nil { | ||
return nil, err | ||
} | ||
defer resp.Body.Close() | ||
|
||
if resp.StatusCode != http.StatusSeeOther { | ||
fullBody, err := ioutil.ReadAll(resp.Body) | ||
if err != nil { | ||
return nil, fmt.Errorf("response status code %d", resp.StatusCode) | ||
} | ||
return nil, fmt.Errorf("response status code %d: %s", resp.StatusCode, fullBody) | ||
} | ||
|
||
redirURL := resp.Header.Get("Location") | ||
if redirURL == "" { | ||
return nil, fmt.Errorf("no redirect URL found in response") | ||
} | ||
u, err := url.Parse(redirURL) | ||
if err != nil { | ||
return nil, err | ||
} | ||
values, err := url.ParseQuery(u.Fragment) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &types.VerifyResponse{ | ||
URL: redirURL, | ||
Error: values.Get("error"), | ||
ErrorCode: values.Get("error_code"), | ||
ErrorDescription: values.Get("error_description"), | ||
}, 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package integration_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/kwoodhouse93/gotrue-go/types" | ||
) | ||
|
||
func TestVerify(t *testing.T) { | ||
assert := assert.New(t) | ||
require := require.New(t) | ||
|
||
// Unauthorized client | ||
resp, err := autoconfirmClient.Verify(types.VerifyRequest{ | ||
Type: types.VerificationTypeSignup, | ||
Token: "abcde", | ||
RedirectTo: "http://localhost:3000", | ||
}) | ||
require.NoError(err) | ||
assert.NotEmpty(resp.URL) | ||
assert.Equal("401", resp.ErrorCode) | ||
assert.Equal("unauthorized_client", resp.Error) | ||
|
||
// Authorized client | ||
// TODO: Test | ||
} |
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