auth

Paddy 2014-12-14 Parent:b0a759e00e6d Child:a8b86f5fba78

96:e57a57a944c4 Browse Files

Introduce Config and NewContext. Config is an exported struct that configures the program. It holds all the values that a Context needs. NewContext takes a Config object and builds a Context out of it.

config.go context.go

     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/config.go	Sun Dec 14 11:59:46 2014 -0500
     1.3 @@ -0,0 +1,24 @@
     1.4 +package auth
     1.5 +
     1.6 +import (
     1.7 +	"errors"
     1.8 +	"html/template"
     1.9 +)
    1.10 +
    1.11 +var (
    1.12 +	// ErrInvalidLoginURI is returned when a Context is instantiated with a Config object that specifies a LoginURI that can't be parsed as a URL.
    1.13 +	ErrInvalidLoginURI = errors.New("invalid login URI")
    1.14 +)
    1.15 +
    1.16 +// Config holds the configuration values necessary to run a server. A Config
    1.17 +// instance is the only way to instantiate a Context variable.
    1.18 +type Config struct {
    1.19 +	ClientStore   clientStore
    1.20 +	AuthCodeStore authorizationCodeStore
    1.21 +	ProfileStore  profileStore
    1.22 +	TokenStore    tokenStore
    1.23 +	SessionStore  sessionStore
    1.24 +	Template      *template.Template
    1.25 +	LoginURI      string
    1.26 +	iterations    int
    1.27 +}
     2.1 --- a/context.go	Sat Dec 13 19:45:38 2014 -0500
     2.2 +++ b/context.go	Sun Dec 14 11:59:46 2014 -0500
     2.3 @@ -21,6 +21,28 @@
     2.4  	profiles  profileStore
     2.5  	tokens    tokenStore
     2.6  	sessions  sessionStore
     2.7 +	config    Config
     2.8 +}
     2.9 +
    2.10 +// NewContext takes a Config instance and uses it to bootstrap a Context
    2.11 +// using the information provided in the Config variable.
    2.12 +func NewContext(config Config) (Context, error) {
    2.13 +	context := Context{
    2.14 +		clients:   config.ClientStore,
    2.15 +		authCodes: config.AuthCodeStore,
    2.16 +		profiles:  config.ProfileStore,
    2.17 +		tokens:    config.TokenStore,
    2.18 +		sessions:  config.SessionStore,
    2.19 +		template:  config.Template,
    2.20 +		config:    config,
    2.21 +	}
    2.22 +	var err error
    2.23 +	context.loginURI, err = url.Parse(config.LoginURI)
    2.24 +	if err != nil {
    2.25 +		log.Println(err)
    2.26 +		return Context{}, ErrInvalidLoginURI
    2.27 +	}
    2.28 +	return context, nil
    2.29  }
    2.30  
    2.31  // Render uses the HTML templates associated with the Context to render the