1
0
mirror of https://github.com/volatiletech/authboss.git synced 2025-02-09 13:47:09 +02:00

started work on auth module. redefined how configuration is going to work and where core is going to reside

This commit is contained in:
Kris Runzer 2015-01-04 10:33:53 -08:00
parent 8ac986b99b
commit 0f691e7607
7 changed files with 154 additions and 4 deletions

3
.gitignore vendored
View File

@ -22,3 +22,6 @@ _testmain.go
*.exe
*.test
*.prof
*.iml
.idea

2
auth/README.md Normal file
View File

@ -0,0 +1,2 @@
Auth
=========

67
auth/auth.go Normal file
View File

@ -0,0 +1,67 @@
package auth
import (
"io"
"net/http"
"path"
"github.com/go-authboss/authboss"
)
const (
methodGET = "GET"
methodPOST = "POST"
)
func init() {
a := &Auth{}
authboss.Register(a)
}
type Auth struct {
Routes authboss.Routes
loginPage io.Reader
logoutRedirect string
}
func (a *Auth) Initialize(c authboss.Config) error {
// create the reader for the default or specified file
var err error
a.loginPage, err = views_login_tpl_bytes()
a.Routes = Routes{
path.Join(c.MountPath, "login"): a.loginHandler,
path.Join(c.MountPath, "logout"): a.logoutHandler,
}
return nil
}
func (a *Auth) Routes() authboss.Routes {
return a.Routes
}
func (a *Auth) Storage() {
}
func (a *Auth) loginHandler(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case methodGET:
io.Copy(w, a.loginPage)
case methodPOST:
// TODO
default:
w.WriteHeader(http.StatusMethodNotAllowed)
}
}
func (a *Auth) logoutHandler(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case methodGET:
http.Redirect(w, r, a.logoutRedirect, http.StatusTemporaryRedirect)
default:
w.WriteHeader(http.StatusMethodNotAllowed)
}
}

44
auth/auth_test.go Normal file
View File

@ -0,0 +1,44 @@
package auth
import (
"bytes"
"io"
"net/http"
"net/http/httptest"
"testing"
)
func TestAuth_loginHandler_GET(t *testing.T) {
tests := []struct {
InLoginPage io.ReadWriter
OutBody string
}{
{nil, htmlLoginPage},
{bytes.NewBufferString("<form></form>"), "<form></form>"},
}
for i, test := range tests {
var c authConfig
if test.InLoginPage == nil {
c = NewAuthConfig()
} else {
c = authConfig{LoginPage: test.InLoginPage}
}
a := NewAuth(c)
w := httptest.NewRecorder()
r, err := http.NewRequest("GET", "/login", nil)
if err != nil {
t.Errorf("%d> Unexpected error: %s", i, err)
}
a.loginHandler(w, r)
if http.StatusOK != w.Code {
t.Errorf("%d> Expected response code 200, got %d", i, w.Code)
}
if test.OutBody != w.Body.String() {
t.Errorf("%d> Expected body '%s', got '%s'", i, test.OutBody, w.Body.String())
}
}
}

11
auth/views/login.tpl Normal file
View File

@ -0,0 +1,11 @@
<html>
<body>
<form method="post" action="/login">
<label for="username">Username</label>
<input type="text" id="username" name="username" />
<label for="password">Password</label>
<input type="password" id="password" name="password" />
<button type="submit">Login</button>
</form>
</body>
</html>

View File

@ -2,7 +2,13 @@ package authboss
// Config is a map to provide configuration key-values.
//
// Config{
// "logger": myIOReader,
// }
type Config map[string]interface{}
type Config struct {
MountPath string `json:"mountPath" xml:"mountPath"`
AuthLoginPageURI string `json:"authLoginPage" xml:"authLoginPage"`
AuthLogoutRedirect string `json:"authLogoutRedirect" xml:"authLogoutRedirect"`
}
func NewConfig() {
}

17
core.go Normal file
View File

@ -0,0 +1,17 @@
package authboss
import (
"net/http"
)
type Routes map[string]http.HandlerFunc
type Modularizer interface {
Initialize(Config) error
Routes() Routes
Storage()
}
func Register(m Modularizer) {
}