mirror of
https://github.com/kataras/iris.git
synced 2026-01-08 20:41:57 +00:00
New basic auth middleware and GetRaw on User (godocs missing)
This commit is contained in:
@@ -1,8 +1,6 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/kataras/iris/v12"
|
||||
"github.com/kataras/iris/v12/middleware/basicauth"
|
||||
)
|
||||
@@ -10,25 +8,40 @@ import (
|
||||
func newApp() *iris.Application {
|
||||
app := iris.New()
|
||||
|
||||
authConfig := basicauth.Config{
|
||||
Users: map[string]string{"myusername": "mypassword", "mySecondusername": "mySecondpassword"},
|
||||
Realm: "Authorization Required", // defaults to "Authorization Required"
|
||||
Expires: time.Duration(30) * time.Minute,
|
||||
}
|
||||
/*
|
||||
opts := basicauth.Options{
|
||||
Realm: "Authorization Required",
|
||||
MaxAge: 30 * time.Minute,
|
||||
GC: basicauth.GC{
|
||||
Every: 2 * time.Hour,
|
||||
},
|
||||
Allow: basicauth.AllowUsers(map[string]string{
|
||||
"myusername": "mypassword",
|
||||
"mySecondusername": "mySecondpassword",
|
||||
}),
|
||||
MaxTries: 2,
|
||||
}
|
||||
auth := basicauth.New(opts)
|
||||
|
||||
authentication := basicauth.New(authConfig)
|
||||
OR simply:
|
||||
*/
|
||||
|
||||
// to global app.Use(authentication) (or app.UseGlobal before the .Run)
|
||||
auth := basicauth.Default(map[string]string{
|
||||
"myusername": "mypassword",
|
||||
"mySecondusername": "mySecondpassword",
|
||||
})
|
||||
|
||||
// to global app.Use(auth) (or app.UseGlobal before the .Run)
|
||||
// to routes
|
||||
/*
|
||||
app.Get("/mysecret", authentication, h)
|
||||
app.Get("/mysecret", auth, h)
|
||||
*/
|
||||
|
||||
app.Get("/", func(ctx iris.Context) { ctx.Redirect("/admin") })
|
||||
|
||||
// to party
|
||||
|
||||
needAuth := app.Party("/admin", authentication)
|
||||
needAuth := app.Party("/admin", auth)
|
||||
{
|
||||
//http://localhost:8080/admin
|
||||
needAuth.Get("/", h)
|
||||
|
||||
@@ -25,5 +25,5 @@ func TestBasicAuth(t *testing.T) {
|
||||
|
||||
// with invalid basic auth
|
||||
e.GET("/admin/settings").WithBasicAuth("invalidusername", "invalidpassword").
|
||||
Expect().Status(httptest.StatusUnauthorized)
|
||||
Expect().Status(httptest.StatusForbidden)
|
||||
}
|
||||
|
||||
@@ -4,7 +4,7 @@ go 1.15
|
||||
|
||||
require (
|
||||
github.com/google/uuid v1.1.2
|
||||
github.com/kataras/iris/v12 v12.2.0-alpha.0.20201106220849-7a19cfb2112f
|
||||
github.com/kataras/iris/v12 v12.2.0-alpha.0.20201113181155-4d09475c290d
|
||||
golang.org/x/crypto v0.0.0-20201016220609-9e8e0b390897
|
||||
)
|
||||
|
||||
|
||||
@@ -17,22 +17,21 @@ func main() {
|
||||
|
||||
newtest := app.Subdomain("newtest")
|
||||
newtest.Get("/", newTestIndex)
|
||||
newtest.Get("/", newTestAbout)
|
||||
newtest.Get("/about", newTestAbout)
|
||||
|
||||
redirects := rewrite.Load("redirects.yml")
|
||||
app.WrapRouter(redirects)
|
||||
|
||||
// http://mydomain.com:8080/seo/about -> http://www.mydomain.com:8080/about
|
||||
// http://test.mydomain.com:8080 -> http://newtest.mydomain.com:8080
|
||||
// http://test.mydomain.com:8080/seo/about -> http://newtest.mydomain.com:8080/about
|
||||
// http://localhost:8080/seo -> http://localhost:8080
|
||||
// http://localhost:8080/about
|
||||
// http://localhost:8080/docs/v12/hello -> http://localhost:8080/docs
|
||||
// http://localhost:8080/docs/v12some -> http://localhost:8080/docs
|
||||
// http://localhost:8080/oldsome -> http://localhost:8080
|
||||
// http://localhost:8080/oldindex/random -> http://localhost:8080
|
||||
// http://localhost:8080/users.json -> http://localhost:8080/users.json
|
||||
// ^ (but with an internal ?format=json, client can't see it)
|
||||
// http://mydomain.com:8080/seo/about -> http://www.mydomain.com:8080/about
|
||||
// http://test.mydomain.com:8080 -> http://newtest.mydomain.com:8080
|
||||
// http://test.mydomain.com:8080/seo/about -> http://newtest.mydomain.com:8080/about
|
||||
// http://mydomain.com:8080/seo -> http://www.mydomain.com:8080
|
||||
// http://mydomain.com:8080/about
|
||||
// http://mydomain.com:8080/docs/v12/hello -> http://www.mydomain.com:8080/docs
|
||||
// http://mydomain.com:8080/docs/v12some -> http://www.mydomain.com:8080/docs
|
||||
// http://mydomain.com:8080/oldsome -> http://www.mydomain.com:8080
|
||||
// http://mydomain.com:8080/oldindex/random -> http://www.mydomain.com:8080
|
||||
// http://mydomain.com:8080/users.json -> http://www.mydomain.com:8080/users?format=json
|
||||
app.Listen(":8080")
|
||||
}
|
||||
|
||||
|
||||
@@ -21,4 +21,4 @@ RedirectMatch: # REDIRECT_CODE_DIGITS | PATTERN_REGEX | TARGET_REPL
|
||||
# Redirects root domain to www.
|
||||
# Creation of a www subdomain inside the Application is unnecessary,
|
||||
# all requests are handled by the root Application itself.
|
||||
PrimarySubdomain: www
|
||||
PrimarySubdomain: www
|
||||
|
||||
Reference in New Issue
Block a user