Skip to content

Commit ce3d822

Browse files
authored
Merge pull request #19 from netlify/wait_until_issued
Add a function to wait until an auth ticket is authorized by the server.
2 parents cca516a + 1e78ee3 commit ce3d822

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

go/porcelain/auth.go

+38
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,19 @@
11
package porcelain
22

33
import (
4+
"fmt"
5+
"time"
6+
7+
"github.com/Sirupsen/logrus"
48
"github.com/netlify/open-api/go/models"
59
"github.com/netlify/open-api/go/plumbing/operations"
610
"github.com/netlify/open-api/go/porcelain/context"
711
)
812

13+
const (
14+
ticketingTimeout = time.Minute * 5
15+
)
16+
917
// Create a login ticket to authenticate a user
1018
func (n *Netlify) CreateTicket(ctx context.Context, clientID string) (*models.Ticket, error) {
1119
params := operations.NewCreateTicketParams().WithClientID(clientID)
@@ -39,3 +47,33 @@ func (n *Netlify) ExchangeTicket(ctx context.Context, ticketID string) (*models.
3947

4048
return resp.Payload, nil
4149
}
50+
51+
func (n *Netlify) WaitUntilTicketAuthorized(ctx context.Context, ticket *models.Ticket) (*models.Ticket, error) {
52+
authInfo := context.GetAuthInfo(ctx)
53+
ticker := time.NewTicker(2 * time.Second)
54+
defer ticker.Stop()
55+
56+
params := operations.NewShowTicketParams().WithTicketID(ticket.ID)
57+
start := time.Now()
58+
for t := range ticker.C {
59+
resp, err := n.Netlify.Operations.ShowTicket(params, authInfo)
60+
if err != nil {
61+
time.Sleep(3 * time.Second)
62+
continue
63+
}
64+
context.GetLogger(ctx).WithFields(logrus.Fields{
65+
"ticket_id": ticket.ID,
66+
"authorized": resp.Payload.Authorized,
67+
}).Debug("Waiting until deploy ready")
68+
69+
if resp.Payload.Authorized {
70+
return resp.Payload, nil
71+
}
72+
73+
if t.Sub(start) > ticketingTimeout {
74+
return nil, fmt.Errorf("Error: the authorization process timed out")
75+
}
76+
}
77+
78+
return ticket, nil
79+
}

0 commit comments

Comments
 (0)