auth

Paddy 2014-09-01

23:1aa3a85ff853 Go to Latest

auth/context.go.old

Deprecate old implementations. Let's remove all of the osin stuff altogether, in favour of a more testable, unit-based approach. Leave all the old files around, for easy reference, but add the .old suffix so the go tools don't pick them up.

History
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/context.go.old	Mon Sep 01 09:13:52 2014 -0400
     1.3 @@ -0,0 +1,108 @@
     1.4 +package auth
     1.5 +
     1.6 +import (
     1.7 +	"encoding/json"
     1.8 +	"html/template"
     1.9 +	"io"
    1.10 +	"log"
    1.11 +	"net/http"
    1.12 +
    1.13 +	"github.com/justinas/nosurf"
    1.14 +)
    1.15 +
    1.16 +type Context struct {
    1.17 +	Config    ServerConfig
    1.18 +	Clients   ClientStore
    1.19 +	Tokens    TokenStore
    1.20 +	Profiles  ProfileStore
    1.21 +	Sessions  SessionStore
    1.22 +	Log       *log.Logger
    1.23 +	Templates Templates
    1.24 +}
    1.25 +
    1.26 +type Templates struct {
    1.27 +	Error        *template.Template
    1.28 +	Confirmation *template.Template
    1.29 +	Login        *template.Template
    1.30 +}
    1.31 +
    1.32 +type jsonError struct {
    1.33 +	Error       string `json:"error,omitempty"`
    1.34 +	Description string `json:"error_description,omitempty"`
    1.35 +	URI         string `json:"error_uri,omitempty"`
    1.36 +	State       string `json:"state,omitempty"`
    1.37 +}
    1.38 +
    1.39 +func (c Context) RenderError(w io.Writer, err error) {
    1.40 +	if c.Templates.Error == nil {
    1.41 +		log.Println("Error template is nil, can't render error.")
    1.42 +		return
    1.43 +	}
    1.44 +	renderErr := c.Templates.Error.Execute(w, map[string]interface{}{
    1.45 +		"err": err,
    1.46 +	})
    1.47 +	if renderErr != nil {
    1.48 +		log.Printf("Error executing error template (oh, the irony): %s\n", renderErr)
    1.49 +		return
    1.50 +	}
    1.51 +}
    1.52 +
    1.53 +func (c Context) RenderJSONError(w io.Writer, code, description, baseURI string) {
    1.54 +	d, err := json.Marshal(jsonError{
    1.55 +		Error:       code,
    1.56 +		Description: description,
    1.57 +		URI:         baseURI,
    1.58 +	})
    1.59 +	if err != nil {
    1.60 +		log.Printf("Error marshalling json error (oh, the irony): %s\n", err)
    1.61 +		return
    1.62 +	}
    1.63 +	_, err = w.Write(d)
    1.64 +	if err != nil {
    1.65 +		log.Printf("Error writing json error: %s\n", err)
    1.66 +		return
    1.67 +	}
    1.68 +}
    1.69 +
    1.70 +func (c Context) RenderConfirmation(w io.Writer, r *http.Request, req AuthRequest) {
    1.71 +	if c.Templates.Confirmation == nil {
    1.72 +		log.Println("Confirmation template is nil, can't render confirmation.")
    1.73 +		return
    1.74 +	}
    1.75 +	err := c.Templates.Confirmation.Execute(w, map[string]interface{}{
    1.76 +		"scope":      req.Scope,
    1.77 +		"client":     req.Client,
    1.78 +		"csrf_token": nosurf.Token(r),
    1.79 +	})
    1.80 +	if err != nil {
    1.81 +		log.Printf("Error executing confirmation template: %s\n", err)
    1.82 +		return
    1.83 +	}
    1.84 +}
    1.85 +
    1.86 +func (c Context) RenderLogin(w io.Writer, r *http.Request) {
    1.87 +	if c.Templates.Login == nil {
    1.88 +		log.Println("Login template is nil, can't render confirmation.")
    1.89 +		return
    1.90 +	}
    1.91 +	err := c.Templates.Login.Execute(w, map[string]interface{}{
    1.92 +		"csrf_token": nosurf.Token(r),
    1.93 +	})
    1.94 +	if err != nil {
    1.95 +		log.Printf("Error executing login template: %s\n", err)
    1.96 +		return
    1.97 +	}
    1.98 +}
    1.99 +
   1.100 +func (c Context) RenderJSONToken(w io.Writer, data AccessData) {
   1.101 +	d, err := json.Marshal(data)
   1.102 +	if err != nil {
   1.103 +		log.Printf("Error marshalling json token: %s\n", err)
   1.104 +		return
   1.105 +	}
   1.106 +	_, err = w.Write(d)
   1.107 +	if err != nil {
   1.108 +		log.Printf("Error writing json token: %s\n", err)
   1.109 +		return
   1.110 +	}
   1.111 +}