auth

Paddy 2015-05-17 Child:b0d1b3e39fc8

172:8ecb60d29b0d Go to Latest

auth/client/client.go

Support email verification. The bulk of this commit is auto-modifying files to export variables (mostly our request error types and our response type) so that they can be reused in a Go client for that API. We also implement the beginnings of a Go client for that API, implementing the bare minimum we need for our immediate purposes: the ability to retrieve information about a Login. This, of course, means we need an API endpoint that will return information about a Login, which in turn required us to implement a GetLogin method in our profileStore. Which got in-memory and postgres implementations. That done, we could add the Verification field and Verified field to the Login type, to keep track of whether we've verified the user's ownership of those communication methods (if the Login is, in fact, a communication method). This required us to update sql/postgres_init.sql to account for the new fields we're tracking. It also means that when creating a Login, we had to generate a UUID to use as the Verification field. To make things complete, we needed a verifyLogin method on the profileStore to mark a Login as verified. That, in turn, required an endpoint to control this through the API. While doing so, I lumped things together in an UpdateLogin handler just so we could reuse the endpoint and logic when resending a verification email that may have never reached the user, for whatever reason (the quintessential "send again" button). Finally, we implemented an email_verification listener that will pull email_verification events off NSQ, check for the requisite data integrity, and use mailgun to email out a verification/welcome email.

History
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/client/client.go	Sun May 17 02:27:36 2015 -0400
     1.3 @@ -0,0 +1,81 @@
     1.4 +package client
     1.5 +
     1.6 +import (
     1.7 +	"bytes"
     1.8 +	"encoding/json"
     1.9 +	"errors"
    1.10 +	"io"
    1.11 +	"net/http"
    1.12 +	"strings"
    1.13 +
    1.14 +	"code.secondbit.org/auth.hg"
    1.15 +)
    1.16 +
    1.17 +var (
    1.18 +	ErrNilClient     = errors.New("nil client wrapper")
    1.19 +	ErrNilHTTPClient = errors.New("nil client")
    1.20 +)
    1.21 +
    1.22 +type Client struct {
    1.23 +	client  *http.Client
    1.24 +	address string
    1.25 +}
    1.26 +
    1.27 +func New(address string) *Client {
    1.28 +	address = strings.TrimRight(address, "/")
    1.29 +	return &Client{
    1.30 +		address: address,
    1.31 +		client:  &http.Client{},
    1.32 +	}
    1.33 +}
    1.34 +
    1.35 +func (c *Client) do(method, url string, request interface{}) (auth.Response, error) {
    1.36 +	if c == nil {
    1.37 +		return auth.Response{}, ErrNilClient
    1.38 +	}
    1.39 +	if c.client == nil {
    1.40 +		return auth.Response{}, ErrNilHTTPClient
    1.41 +	}
    1.42 +	var response auth.Response
    1.43 +	if !strings.HasPrefix(url, "http") {
    1.44 +		url = strings.TrimLeft(url, "/")
    1.45 +		url = "/" + url
    1.46 +		url = c.address + url
    1.47 +	}
    1.48 +	var body io.Reader
    1.49 +	if request != nil {
    1.50 +		data, err := json.Marshal(request)
    1.51 +		if err != nil {
    1.52 +			return response, err
    1.53 +		}
    1.54 +		body = bytes.NewBuffer(data)
    1.55 +	}
    1.56 +	req, err := http.NewRequest(method, url, body)
    1.57 +	if err != nil {
    1.58 +		return response, err
    1.59 +	}
    1.60 +	resp, err := c.client.Do(req)
    1.61 +	if err != nil {
    1.62 +		return response, err
    1.63 +	}
    1.64 +	defer resp.Body.Close()
    1.65 +	switch resp.Header.Get("Content-Type") {
    1.66 +	case "application/json":
    1.67 +		dec := json.NewDecoder(resp.Body)
    1.68 +		err = dec.Decode(&response)
    1.69 +		if err != nil {
    1.70 +			return response, err
    1.71 +		}
    1.72 +	default:
    1.73 +		dec := json.NewDecoder(resp.Body)
    1.74 +		err = dec.Decode(&response)
    1.75 +		if err != nil {
    1.76 +			return response, err
    1.77 +		}
    1.78 +	}
    1.79 +	return response, nil
    1.80 +}
    1.81 +
    1.82 +func (c *Client) Get(url string) (auth.Response, error) {
    1.83 +	return c.do("GET", url, nil)
    1.84 +}