Skip to content

Commit

Permalink
Set expiry
Browse files Browse the repository at this point in the history
  • Loading branch information
ngs committed Feb 16, 2018
1 parent dda9ca1 commit da51db1
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
2 changes: 2 additions & 0 deletions app/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package app
import (
"encoding/json"
"net/http"
"time"

"golang.org/x/oauth2"

Expand Down Expand Up @@ -69,6 +70,7 @@ func (app *App) HandleOAuthCallback(w http.ResponseWriter, r *http.Request) {
return
}
ctx.UserID = ctx.GetUserIDForState(state)
token.Expiry = time.Now().Add(time.Hour) // SalesForce always returns zero-expiry, but it expires.
ctx.SetAccessToken(token)
ctx.DeleteState(state)
http.Redirect(w, r, "/success", http.StatusFound)
Expand Down
10 changes: 10 additions & 0 deletions app/routes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"strconv"
"strings"
"testing"
"time"

"github.com/gorilla/mux"
"golang.org/x/oauth2"
Expand Down Expand Up @@ -110,13 +111,15 @@ func TestHandleAuthenticateNotFound(t *testing.T) {

func TestHandleOAuthCallback(t *testing.T) {
defer gock.Off()
expiry, _ := time.Parse("2016-01-02T15:04:05Z", "0001-01-01T00:00:00Z")
gock.New("https://login.salesforce.com").
Post("/services/oauth2/token").
Reply(200).
JSON(oauth2.Token{
AccessToken: "foo",
RefreshToken: "bar",
TokenType: "Bearer",
Expiry: expiry,
})

app := createMockApp()
Expand All @@ -126,10 +129,17 @@ func TestHandleOAuthCallback(t *testing.T) {
ctx := app.CreateContext(req)
ctx.UserID = "FOO"
state, _ := ctx.StoreUserIDInState()
token := ctx.GetAccessTokenForUser()
Test{true, token == nil}.Compare(t)
req, _ = http.NewRequest(http.MethodGet, "https://example.com/oauth/callback?state="+state+"&code=fjkfjk", nil)
app.SetupRouter().ServeHTTP(res, req)
token = ctx.GetAccessTokenForUser()
for _, test := range []Test{
{302, res.Code},
{false, token == nil},
{"bar", token.RefreshToken},
{"foo", token.AccessToken},
{false, token.Expiry.IsZero()},
{"/success", res.Header().Get("Location")},
} {
test.Compare(t)
Expand Down

0 comments on commit da51db1

Please sign in to comment.