mirror of
https://github.com/volatiletech/authboss.git
synced 2024-11-24 08:42:17 +02:00
Fix CurrentUser to pull the current user.
- Make slightly nicer tests for camelcasing. - Fix an issue with oauth url generation.
This commit is contained in:
parent
06edd2e615
commit
fb97394c7f
@ -10,6 +10,7 @@ package authboss // import "gopkg.in/authboss.v0"
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// Init authboss and it's loaded modules.
|
||||
@ -54,7 +55,11 @@ func CurrentUser(w http.ResponseWriter, r *http.Request) (interface{}, error) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return Cfg.Storer.Get(key, ModuleAttrMeta)
|
||||
if index := strings.IndexByte(key, ';'); index > 0 {
|
||||
return Cfg.OAuth2Storer.GetOAuth(key[:index], key[index+1:], ModuleAttrMeta)
|
||||
} else {
|
||||
return Cfg.Storer.Get(key, ModuleAttrMeta)
|
||||
}
|
||||
}
|
||||
|
||||
// CurrentUserP retrieves the current user but panics if it's not available for
|
||||
|
@ -41,14 +41,13 @@ func (o *OAuth2) Routes() authboss.RouteTable {
|
||||
init := fmt.Sprintf("/oauth2/%s", prov)
|
||||
callback := fmt.Sprintf("/oauth2/callback/%s", prov)
|
||||
|
||||
if len(authboss.Cfg.MountPath) > 0 {
|
||||
init = path.Join(authboss.Cfg.MountPath, init)
|
||||
callback = path.Join(authboss.Cfg.MountPath, callback)
|
||||
}
|
||||
|
||||
routes[init] = oauthInit
|
||||
routes[callback] = oauthCallback
|
||||
|
||||
if len(authboss.Cfg.MountPath) > 0 {
|
||||
callback = path.Join(authboss.Cfg.MountPath, callback)
|
||||
}
|
||||
|
||||
cfg.OAuth2Config.RedirectURL = authboss.Cfg.RootURL + callback
|
||||
}
|
||||
|
||||
@ -126,6 +125,7 @@ func oauthCallback(ctx *authboss.Context, w http.ResponseWriter, r *http.Request
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
ctx.SessionStorer.Del(authboss.SessionOAuth2State)
|
||||
|
||||
cfg, ok := authboss.Cfg.OAuth2Providers[provider]
|
||||
if !ok {
|
||||
|
@ -55,8 +55,8 @@ func TestRoutes(t *testing.T) {
|
||||
|
||||
o := OAuth2{}
|
||||
routes := o.Routes()
|
||||
authURL := path.Join(mount, "oauth2", "google")
|
||||
tokenURL := path.Join(mount, "oauth2", "callback", "google")
|
||||
authURL := path.Join("/oauth2", "google")
|
||||
tokenURL := path.Join("/oauth2", "callback", "google")
|
||||
redir := root + path.Join(mount, "oauth2", "callback", "google")
|
||||
|
||||
if _, ok := routes[authURL]; !ok {
|
||||
@ -205,6 +205,9 @@ func TestOAuthSuccess(t *testing.T) {
|
||||
if val, _ := session.Get(authboss.SessionKey); val != "uid;fake" {
|
||||
t.Error("User was not logged in:", val)
|
||||
}
|
||||
if _, ok := session.Get(authboss.SessionOAuth2State); ok {
|
||||
t.Error("Expected state to be deleted.")
|
||||
}
|
||||
|
||||
if w.Code != http.StatusFound {
|
||||
t.Error("It should redirect")
|
||||
|
@ -413,15 +413,22 @@ func TestUnbind_Valuer(t *testing.T) {
|
||||
func TestCasingStyleConversions(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
camel := "SomethingInCamel"
|
||||
|
||||
got := camelToUnder(camel)
|
||||
if got != "something_in_camel" {
|
||||
t.Error(got)
|
||||
tests := []struct {
|
||||
In string
|
||||
Out string
|
||||
}{
|
||||
{"SomethingInCamel", "something_in_camel"},
|
||||
{"Oauth2Anything", "oauth2_anything"},
|
||||
}
|
||||
|
||||
got = underToCamel(got)
|
||||
if got != camel {
|
||||
t.Error(got)
|
||||
for i, test := range tests {
|
||||
out := camelToUnder(test.In)
|
||||
if out != test.Out {
|
||||
t.Errorf("%d) Expected %q got %q", i, test.Out, out)
|
||||
}
|
||||
out = underToCamel(out)
|
||||
if out != test.In {
|
||||
t.Error("%d), Expected %q got %q", i, test.In, out)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user