auth

Paddy 2015-12-14 Parent:0a2c3d677161

181:b7e685839a1b Go to Latest

auth/config.go

Break out scopes and events. This repo has gotten unwieldy, and there are portions of it that need to be imported by a large number of other packages. For example, scopes will be used in almost every API we write. Rather than importing the entirety of this codebase into every API we write, I've opted to move the scope logic out into a scopes package, with a subpackage for the defined types, which is all most projects actually want to import. We also define some event type constants, and importing those shouldn't require a project to import all our dependencies, either. So I made an events subpackage that just holds those constants. This package has become a little bit of a red-headed stepchild and is do for a refactor, but I'm trying to put that off as long as I can. The refactoring of our scopes stuff has left a bug wherein a token can be granted for scopes that don't exist. I'm going to need to revisit that, and also how to limit scopes to only be granted to the users that should be able to request them. But that's a battle for another day.

History
1 package auth
3 import (
4 "errors"
5 "html/template"
6 "log"
8 "code.secondbit.org/events.hg"
9 )
11 var (
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.
13 ErrInvalidLoginURI = errors.New("invalid login URI")
14 // ErrConfigNotInitialized is returned when a Context is instantiated with a Config object that hasn't had its Init function called.
15 ErrConfigNotInitialized = errors.New("config not initialized")
17 // Version is used to keep track of what version of the build this is
18 Version string
19 )
21 // Config holds the configuration values necessary to run a server. A Config
22 // instance is the only way to instantiate a Context variable.
23 type Config struct {
24 ClientStore clientStore
25 AuthCodeStore authorizationCodeStore
26 ProfileStore profileStore
27 TokenStore tokenStore
28 SessionStore sessionStore
29 EventsPublisher events.Publisher
30 Template *template.Template
31 LoginURI string
32 JWTPrivateKey []byte
33 iterations int
34 secureCookie bool
35 }
37 // Init is a function that preps the Config object to be used for Context creation, setting variables
38 // that are determined at the beginning of program execution.
39 func (c *Config) Init() error {
40 scheme, ok := passphraseSchemes[CurPassphraseScheme]
41 if !ok {
42 return ErrInvalidPassphraseScheme
43 }
44 var err error
45 c.iterations, err = scheme.calculateIterations()
46 if err != nil {
47 return err
48 }
49 log.Printf("Generating passphrases with %d iterations...\n", c.iterations)
50 return nil
51 }