Skip to content

Commit

Permalink
progress
Browse files Browse the repository at this point in the history
  • Loading branch information
autom8ter committed Dec 15, 2020
1 parent 69d6994 commit df4424c
Showing 1 changed file with 14 additions and 26 deletions.
40 changes: 14 additions & 26 deletions gql/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,20 @@ import (
"github.com/99designs/gqlgen/graphql/handler/lru"
"github.com/99designs/gqlgen/graphql/handler/transport"
"github.com/autom8ter/machine"
"github.com/gorilla/sessions"
"github.com/gorilla/websocket"
"github.com/graphikDB/graphik/gen/gql/go/generated"
"github.com/graphikDB/graphik/gen/grpc/go"
"github.com/graphikDB/graphik/generic"
"github.com/graphikDB/graphik/helpers"
"github.com/graphikDB/graphik/logger"
"github.com/pkg/errors"
"github.com/rogpeppe/go-internal/cache"
"github.com/rs/cors"
"github.com/segmentio/ksuid"
"go.uber.org/zap"
"golang.org/x/oauth2"
"google.golang.org/grpc/metadata"
"html/template"
"net/http"
"os"
"time"
)

Expand All @@ -43,19 +40,19 @@ type Resolver struct {
machine *machine.Machine
store *generic.Cache
config *oauth2.Config
tokenCookie string
tokenCookie string
stateCookie string
}

func NewResolver(machine *machine.Machine, client apipb.DatabaseServiceClient, cors *cors.Cors, config *oauth2.Config) *Resolver {
return &Resolver{
client: client,
cors: cors,
machine: machine,
machine: machine,
config: config,
tokenCookie: "graphik-playground-token",
tokenCookie: "graphik-playground-token",
stateCookie: "graphik-playground-state",
store: generic.NewCache(machine, 5 *time.Minute),
store: generic.NewCache(machine, 5*time.Minute),
}
}

Expand Down Expand Up @@ -203,16 +200,8 @@ func (r *Resolver) Playground() http.HandlerFunc {

func (r *Resolver) redirectLogin(w http.ResponseWriter, req *http.Request) {
state := helpers.Hash([]byte(ksuid.New().String()))

sess.Values["state"] = state
r.setState(w, state)
redirect := r.config.AuthCodeURL(state)
if err := sess.Save(req, w); err != nil {
logger.Error("playground: failed to save session", zap.Error(err))
if sess2, err := r.store.New(req, r.cookieName); err == nil {
r.redirectLogin(sess2, w, req)
return
}
}
http.Redirect(w, req, redirect, http.StatusTemporaryRedirect)
}

Expand Down Expand Up @@ -275,15 +264,14 @@ func (r *Resolver) getToken(req *http.Request) (*oauth2.Token, error) {

func (r *Resolver) setToken(w http.ResponseWriter, token *oauth2.Token) {
id := helpers.Hash([]byte(ksuid.New().String()))
r.store.Set(id, token, 1 *time.Hour)
r.store.Set(id, token, 1*time.Hour)
http.SetCookie(w, &http.Cookie{
Name: r.tokenCookie,
Value: id,
Expires: time.Now().Add(1 *time.Hour),
Name: r.tokenCookie,
Value: id,
Expires: time.Now().Add(1 * time.Hour),
})
}


func (r *Resolver) getState(req *http.Request) (string, error) {
cookie, err := req.Cookie(r.stateCookie)
if err != nil {
Expand All @@ -298,10 +286,10 @@ func (r *Resolver) getState(req *http.Request) (string, error) {

func (r *Resolver) setState(w http.ResponseWriter, state string) {
id := helpers.Hash([]byte(ksuid.New().String()))
r.store.Set(id, state, 15 *time.Minute)
r.store.Set(id, state, 15*time.Minute)
http.SetCookie(w, &http.Cookie{
Name: r.tokenCookie,
Value: id,
Expires: time.Now().Add(1 *time.Hour),
Name: r.tokenCookie,
Value: id,
Expires: time.Now().Add(1 * time.Hour),
})
}
}

0 comments on commit df4424c

Please sign in to comment.