mirror of
				https://github.com/volatiletech/authboss.git
				synced 2025-10-30 23:47:59 +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:
		
							
								
								
									
										3
									
								
								.gitignore
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										3
									
								
								.gitignore
									
									
									
									
										vendored
									
									
								
							| @@ -22,3 +22,6 @@ _testmain.go | ||||
| *.exe | ||||
| *.test | ||||
| *.prof | ||||
|  | ||||
| *.iml | ||||
| .idea | ||||
							
								
								
									
										2
									
								
								auth/README.md
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										2
									
								
								auth/README.md
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,2 @@ | ||||
| Auth | ||||
| ========= | ||||
							
								
								
									
										67
									
								
								auth/auth.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										67
									
								
								auth/auth.go
									
									
									
									
									
										Normal 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
									
								
							
							
						
						
									
										44
									
								
								auth/auth_test.go
									
									
									
									
									
										Normal 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
									
								
							
							
						
						
									
										11
									
								
								auth/views/login.tpl
									
									
									
									
									
										Normal 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> | ||||
							
								
								
									
										14
									
								
								config.go
									
									
									
									
									
								
							
							
						
						
									
										14
									
								
								config.go
									
									
									
									
									
								
							| @@ -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() { | ||||
|  | ||||
| } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user