mirror of
https://github.com/volatiletech/authboss.git
synced 2026-06-19 23:00:27 +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:
@@ -10,6 +10,7 @@ package authboss // import "gopkg.in/authboss.v0"
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Init authboss and it's loaded modules.
|
// Init authboss and it's loaded modules.
|
||||||
@@ -54,8 +55,12 @@ func CurrentUser(w http.ResponseWriter, r *http.Request) (interface{}, error) {
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if index := strings.IndexByte(key, ';'); index > 0 {
|
||||||
|
return Cfg.OAuth2Storer.GetOAuth(key[:index], key[index+1:], ModuleAttrMeta)
|
||||||
|
} else {
|
||||||
return Cfg.Storer.Get(key, ModuleAttrMeta)
|
return Cfg.Storer.Get(key, ModuleAttrMeta)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// CurrentUserP retrieves the current user but panics if it's not available for
|
// CurrentUserP retrieves the current user but panics if it's not available for
|
||||||
// any reason.
|
// any reason.
|
||||||
|
|||||||
+5
-5
@@ -41,14 +41,13 @@ func (o *OAuth2) Routes() authboss.RouteTable {
|
|||||||
init := fmt.Sprintf("/oauth2/%s", prov)
|
init := fmt.Sprintf("/oauth2/%s", prov)
|
||||||
callback := fmt.Sprintf("/oauth2/callback/%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[init] = oauthInit
|
||||||
routes[callback] = oauthCallback
|
routes[callback] = oauthCallback
|
||||||
|
|
||||||
|
if len(authboss.Cfg.MountPath) > 0 {
|
||||||
|
callback = path.Join(authboss.Cfg.MountPath, callback)
|
||||||
|
}
|
||||||
|
|
||||||
cfg.OAuth2Config.RedirectURL = authboss.Cfg.RootURL + 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 {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
ctx.SessionStorer.Del(authboss.SessionOAuth2State)
|
||||||
|
|
||||||
cfg, ok := authboss.Cfg.OAuth2Providers[provider]
|
cfg, ok := authboss.Cfg.OAuth2Providers[provider]
|
||||||
if !ok {
|
if !ok {
|
||||||
|
|||||||
@@ -55,8 +55,8 @@ func TestRoutes(t *testing.T) {
|
|||||||
|
|
||||||
o := OAuth2{}
|
o := OAuth2{}
|
||||||
routes := o.Routes()
|
routes := o.Routes()
|
||||||
authURL := path.Join(mount, "oauth2", "google")
|
authURL := path.Join("/oauth2", "google")
|
||||||
tokenURL := path.Join(mount, "oauth2", "callback", "google")
|
tokenURL := path.Join("/oauth2", "callback", "google")
|
||||||
redir := root + path.Join(mount, "oauth2", "callback", "google")
|
redir := root + path.Join(mount, "oauth2", "callback", "google")
|
||||||
|
|
||||||
if _, ok := routes[authURL]; !ok {
|
if _, ok := routes[authURL]; !ok {
|
||||||
@@ -205,6 +205,9 @@ func TestOAuthSuccess(t *testing.T) {
|
|||||||
if val, _ := session.Get(authboss.SessionKey); val != "uid;fake" {
|
if val, _ := session.Get(authboss.SessionKey); val != "uid;fake" {
|
||||||
t.Error("User was not logged in:", val)
|
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 {
|
if w.Code != http.StatusFound {
|
||||||
t.Error("It should redirect")
|
t.Error("It should redirect")
|
||||||
|
|||||||
+15
-8
@@ -413,15 +413,22 @@ func TestUnbind_Valuer(t *testing.T) {
|
|||||||
func TestCasingStyleConversions(t *testing.T) {
|
func TestCasingStyleConversions(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|
||||||
camel := "SomethingInCamel"
|
tests := []struct {
|
||||||
|
In string
|
||||||
got := camelToUnder(camel)
|
Out string
|
||||||
if got != "something_in_camel" {
|
}{
|
||||||
t.Error(got)
|
{"SomethingInCamel", "something_in_camel"},
|
||||||
|
{"Oauth2Anything", "oauth2_anything"},
|
||||||
}
|
}
|
||||||
|
|
||||||
got = underToCamel(got)
|
for i, test := range tests {
|
||||||
if got != camel {
|
out := camelToUnder(test.In)
|
||||||
t.Error(got)
|
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)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user