Skip to content
Snippets Groups Projects
Select Git revision
  • 56526c76b563d004f07fe0c538fff626929067de
  • ballinvoher default protected
  • client-http-server-for-token
  • master
  • gitlab-auth-issue
  • windows
  • microsoft
  • message
  • azure_auth
  • prometheus
  • permission-templates
  • no-datastore
  • save-public-keys
  • gitlab-group-level-start
  • v1.1.0
  • v1.0.0
  • v0.1
17 results

main.go

Blame
  • main.go 6.72 KiB
    package main
    
    import (
    	"crypto/rand"
    	"encoding/hex"
    	"encoding/json"
    	"errors"
    	"flag"
    	"fmt"
    	"html/template"
    	"io"
    	"io/ioutil"
    	"log"
    	"net/http"
    	"time"
    
    	"golang.org/x/oauth2"
    
    	"github.com/dgrijalva/jwt-go"
    	"github.com/gorilla/mux"
    	"github.com/gorilla/sessions"
    	"github.com/nsheridan/cashier/lib"
    	"github.com/nsheridan/cashier/server/auth"
    	"github.com/nsheridan/cashier/server/auth/google"
    	"github.com/nsheridan/cashier/server/config"
    	"github.com/nsheridan/cashier/server/signer"
    )
    
    var (
    	cfg = flag.String("config_file", "config.json", "Path to configuration file.")
    )
    
    // appContext contains local context - cookiestore, authprovider, authsession, templates etc.
    type appContext struct {
    	cookiestore   *sessions.CookieStore
    	authprovider  auth.Provider
    	authsession   *auth.Session
    	views         *template.Template
    	sshKeySigner  *signer.KeySigner
    	jwtSigningKey []byte
    }
    
    // getAuthCookie retrieves a the cookie from the request and validates it.
    func (a *appContext) getAuthCookie(r *http.Request) *oauth2.Token {
    	session, _ := a.cookiestore.Get(r, "tok")
    	t, ok := session.Values["token"]
    	if !ok {
    		return nil
    	}
    	var tok oauth2.Token
    	if err := json.Unmarshal(t.([]byte), &tok); err != nil {
    		return nil
    	}
    	if !a.authprovider.Valid(&tok) {
    		return nil
    	}
    	return &tok
    }
    
    // setAuthCookie marshals the auth token and stores it as a cookie.
    func (a *appContext) setAuthCookie(w http.ResponseWriter, r *http.Request, t *oauth2.Token) {
    	session, _ := a.cookiestore.Get(r, "tok")
    	val, _ := json.Marshal(t)
    	session.Values["token"] = val
    	session.Save(r, w)
    }
    
    // parseKey retrieves and unmarshals the signing request.
    func parseKey(r *http.Request) (*lib.SignRequest, error) {
    	var s lib.SignRequest