2020-06-07 14:26:06 +02:00
|
|
|
// Package basicauth provides http basic authentication via middleware. See _examples/auth/basicauth
|
2017-02-14 04:54:11 +01:00
|
|
|
package basicauth
|
|
|
|
|
2020-10-12 14:52:53 +02:00
|
|
|
/*
|
|
|
|
Test files:
|
|
|
|
- ../../_examples/auth/basicauth/main_test.go
|
|
|
|
- ./basicauth_test.go
|
|
|
|
*/
|
2017-07-10 17:32:42 +02:00
|
|
|
|
2017-02-14 04:54:11 +01:00
|
|
|
import (
|
|
|
|
"encoding/base64"
|
|
|
|
"strconv"
|
2020-08-15 14:40:41 +02:00
|
|
|
"sync"
|
2017-02-14 04:54:11 +01:00
|
|
|
"time"
|
|
|
|
|
2019-10-25 00:27:02 +02:00
|
|
|
"github.com/kataras/iris/v12/context"
|
2017-02-14 04:54:11 +01:00
|
|
|
)
|
|
|
|
|
2020-04-28 04:42:23 +02:00
|
|
|
func init() {
|
2020-04-28 21:34:36 +02:00
|
|
|
context.SetHandlerName("iris/middleware/basicauth.*", "iris.basicauth")
|
2020-04-28 04:42:23 +02:00
|
|
|
}
|
|
|
|
|
2020-10-12 14:52:53 +02:00
|
|
|
const authorizationType = "Basic Authentication"
|
|
|
|
|
2017-02-14 04:54:11 +01:00
|
|
|
type (
|
|
|
|
encodedUser struct {
|
2020-10-12 14:52:53 +02:00
|
|
|
HeaderValue string
|
|
|
|
Username string
|
|
|
|
Password string
|
|
|
|
logged bool
|
|
|
|
forceLogout bool // in order to be able to invalidate and use a redirect response.
|
|
|
|
authorizedAt time.Time // when from !logged to logged.
|
|
|
|
expires time.Time
|
|
|
|
mu sync.RWMutex
|
2017-02-14 04:54:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
basicAuthMiddleware struct {
|
2020-10-12 01:07:04 +02:00
|
|
|
config *Config
|
2017-02-14 04:54:11 +01:00
|
|
|
// these are filled from the config.Users map at the startup
|
2020-10-12 01:07:04 +02:00
|
|
|
auth []*encodedUser
|
2017-02-14 04:54:11 +01:00
|
|
|
realmHeaderValue string
|
2018-08-06 03:20:59 +02:00
|
|
|
|
|
|
|
// The below can be removed but they are here because on the future we may add dynamic options for those two fields,
|
|
|
|
// it is a bit faster to check the b.$bool as well.
|
|
|
|
expireEnabled bool // if the config.Expires is a valid date, default is disabled.
|
|
|
|
askHandlerEnabled bool // if the config.OnAsk is not nil, defaults to false.
|
2017-02-14 04:54:11 +01:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
//
|
|
|
|
|
2017-06-10 14:28:09 +02:00
|
|
|
// New accepts basicauth.Config and returns a new Handler
|
|
|
|
// which will ask the client for basic auth (username, password),
|
|
|
|
// validate that and if valid continues to the next handler, otherwise
|
|
|
|
// throws a StatusUnauthorized http error code.
|
2020-10-12 14:52:53 +02:00
|
|
|
//
|
|
|
|
// Use the `Context.User` method to retrieve the stored user.
|
Publish the new version :airplane: | Look description please!
# FAQ
### Looking for free support?
http://support.iris-go.com
https://kataras.rocket.chat/channel/iris
### Looking for previous versions?
https://github.com/kataras/iris#version
### Should I upgrade my Iris?
Developers are not forced to upgrade if they don't really need it. Upgrade whenever you feel ready.
> Iris uses the [vendor directory](https://docs.google.com/document/d/1Bz5-UB7g2uPBdOx-rw5t9MxJwkfpx90cqG9AFL0JAYo) feature, so you get truly reproducible builds, as this method guards against upstream renames and deletes.
**How to upgrade**: Open your command-line and execute this command: `go get -u github.com/kataras/iris`.
For further installation support, please click [here](http://support.iris-go.com/d/16-how-to-install-iris-web-framework).
### About our new home page
http://iris-go.com
Thanks to [Santosh Anand](https://github.com/santoshanand) the http://iris-go.com has been upgraded and it's really awesome!
[Santosh](https://github.com/santoshanand) is a freelancer, he has a great knowledge of nodejs and express js, Android, iOS, React Native, Vue.js etc, if you need a developer to find or create a solution for your problem or task, please contact with him.
The amount of the next two or three donations you'll send they will be immediately transferred to his own account balance, so be generous please!
Read more at https://github.com/kataras/iris/blob/master/HISTORY.md
Former-commit-id: eec2d71bbe011d6b48d2526eb25919e36e5ad94e
2017-06-03 22:22:52 +02:00
|
|
|
func New(c Config) context.Handler {
|
|
|
|
config := DefaultConfig()
|
|
|
|
if c.Realm != "" {
|
|
|
|
config.Realm = c.Realm
|
|
|
|
}
|
|
|
|
config.Users = c.Users
|
2018-01-21 06:14:51 +01:00
|
|
|
config.Expires = c.Expires
|
2018-08-06 03:20:59 +02:00
|
|
|
config.OnAsk = c.OnAsk
|
Publish the new version :airplane: | Look description please!
# FAQ
### Looking for free support?
http://support.iris-go.com
https://kataras.rocket.chat/channel/iris
### Looking for previous versions?
https://github.com/kataras/iris#version
### Should I upgrade my Iris?
Developers are not forced to upgrade if they don't really need it. Upgrade whenever you feel ready.
> Iris uses the [vendor directory](https://docs.google.com/document/d/1Bz5-UB7g2uPBdOx-rw5t9MxJwkfpx90cqG9AFL0JAYo) feature, so you get truly reproducible builds, as this method guards against upstream renames and deletes.
**How to upgrade**: Open your command-line and execute this command: `go get -u github.com/kataras/iris`.
For further installation support, please click [here](http://support.iris-go.com/d/16-how-to-install-iris-web-framework).
### About our new home page
http://iris-go.com
Thanks to [Santosh Anand](https://github.com/santoshanand) the http://iris-go.com has been upgraded and it's really awesome!
[Santosh](https://github.com/santoshanand) is a freelancer, he has a great knowledge of nodejs and express js, Android, iOS, React Native, Vue.js etc, if you need a developer to find or create a solution for your problem or task, please contact with him.
The amount of the next two or three donations you'll send they will be immediately transferred to his own account balance, so be generous please!
Read more at https://github.com/kataras/iris/blob/master/HISTORY.md
Former-commit-id: eec2d71bbe011d6b48d2526eb25919e36e5ad94e
2017-06-03 22:22:52 +02:00
|
|
|
|
2020-10-12 01:07:04 +02:00
|
|
|
b := &basicAuthMiddleware{config: &config}
|
2017-02-14 04:54:11 +01:00
|
|
|
b.init()
|
|
|
|
return b.Serve
|
|
|
|
}
|
|
|
|
|
2017-06-10 14:28:09 +02:00
|
|
|
// Default accepts only the users and returns a new Handler
|
|
|
|
// which will ask the client for basic auth (username, password),
|
|
|
|
// validate that and if valid continues to the next handler, otherwise
|
|
|
|
// throws a StatusUnauthorized http error code.
|
Publish the new version :airplane: | Look description please!
# FAQ
### Looking for free support?
http://support.iris-go.com
https://kataras.rocket.chat/channel/iris
### Looking for previous versions?
https://github.com/kataras/iris#version
### Should I upgrade my Iris?
Developers are not forced to upgrade if they don't really need it. Upgrade whenever you feel ready.
> Iris uses the [vendor directory](https://docs.google.com/document/d/1Bz5-UB7g2uPBdOx-rw5t9MxJwkfpx90cqG9AFL0JAYo) feature, so you get truly reproducible builds, as this method guards against upstream renames and deletes.
**How to upgrade**: Open your command-line and execute this command: `go get -u github.com/kataras/iris`.
For further installation support, please click [here](http://support.iris-go.com/d/16-how-to-install-iris-web-framework).
### About our new home page
http://iris-go.com
Thanks to [Santosh Anand](https://github.com/santoshanand) the http://iris-go.com has been upgraded and it's really awesome!
[Santosh](https://github.com/santoshanand) is a freelancer, he has a great knowledge of nodejs and express js, Android, iOS, React Native, Vue.js etc, if you need a developer to find or create a solution for your problem or task, please contact with him.
The amount of the next two or three donations you'll send they will be immediately transferred to his own account balance, so be generous please!
Read more at https://github.com/kataras/iris/blob/master/HISTORY.md
Former-commit-id: eec2d71bbe011d6b48d2526eb25919e36e5ad94e
2017-06-03 22:22:52 +02:00
|
|
|
func Default(users map[string]string) context.Handler {
|
2017-02-14 04:54:11 +01:00
|
|
|
c := DefaultConfig()
|
|
|
|
c.Users = users
|
|
|
|
return New(c)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *basicAuthMiddleware) init() {
|
|
|
|
// pass the encoded users from the user's config's Users value
|
2020-10-12 01:07:04 +02:00
|
|
|
b.auth = make([]*encodedUser, 0, len(b.config.Users))
|
2017-02-14 04:54:11 +01:00
|
|
|
|
|
|
|
for k, v := range b.config.Users {
|
|
|
|
fullUser := k + ":" + v
|
|
|
|
header := "Basic " + base64.StdEncoding.EncodeToString([]byte(fullUser))
|
2020-10-12 14:52:53 +02:00
|
|
|
b.auth = append(b.auth, &encodedUser{
|
|
|
|
HeaderValue: header,
|
|
|
|
Username: k,
|
|
|
|
Password: v,
|
|
|
|
logged: false,
|
|
|
|
expires: DefaultExpireTime,
|
|
|
|
})
|
2017-02-14 04:54:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// set the auth realm header's value
|
|
|
|
b.realmHeaderValue = "Basic realm=" + strconv.Quote(b.config.Realm)
|
|
|
|
|
2018-08-06 03:20:59 +02:00
|
|
|
b.expireEnabled = b.config.Expires > 0
|
|
|
|
b.askHandlerEnabled = b.config.OnAsk != nil
|
2017-02-14 04:54:11 +01:00
|
|
|
}
|
|
|
|
|
2020-06-21 16:15:28 +02:00
|
|
|
func (b *basicAuthMiddleware) findAuth(headerValue string) (*encodedUser, bool) {
|
|
|
|
if headerValue != "" {
|
|
|
|
for _, user := range b.auth {
|
|
|
|
if user.HeaderValue == headerValue {
|
|
|
|
return user, true
|
|
|
|
}
|
2017-02-14 04:54:11 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-21 16:15:28 +02:00
|
|
|
return nil, false
|
2017-02-14 04:54:11 +01:00
|
|
|
}
|
|
|
|
|
2020-07-10 22:21:09 +02:00
|
|
|
func (b *basicAuthMiddleware) askForCredentials(ctx *context.Context) {
|
Publish the new version :airplane: | Look description please!
# FAQ
### Looking for free support?
http://support.iris-go.com
https://kataras.rocket.chat/channel/iris
### Looking for previous versions?
https://github.com/kataras/iris#version
### Should I upgrade my Iris?
Developers are not forced to upgrade if they don't really need it. Upgrade whenever you feel ready.
> Iris uses the [vendor directory](https://docs.google.com/document/d/1Bz5-UB7g2uPBdOx-rw5t9MxJwkfpx90cqG9AFL0JAYo) feature, so you get truly reproducible builds, as this method guards against upstream renames and deletes.
**How to upgrade**: Open your command-line and execute this command: `go get -u github.com/kataras/iris`.
For further installation support, please click [here](http://support.iris-go.com/d/16-how-to-install-iris-web-framework).
### About our new home page
http://iris-go.com
Thanks to [Santosh Anand](https://github.com/santoshanand) the http://iris-go.com has been upgraded and it's really awesome!
[Santosh](https://github.com/santoshanand) is a freelancer, he has a great knowledge of nodejs and express js, Android, iOS, React Native, Vue.js etc, if you need a developer to find or create a solution for your problem or task, please contact with him.
The amount of the next two or three donations you'll send they will be immediately transferred to his own account balance, so be generous please!
Read more at https://github.com/kataras/iris/blob/master/HISTORY.md
Former-commit-id: eec2d71bbe011d6b48d2526eb25919e36e5ad94e
2017-06-03 22:22:52 +02:00
|
|
|
ctx.Header("WWW-Authenticate", b.realmHeaderValue)
|
2020-08-28 15:02:14 +02:00
|
|
|
ctx.StatusCode(401)
|
2018-08-06 03:20:59 +02:00
|
|
|
if b.askHandlerEnabled {
|
|
|
|
b.config.OnAsk(ctx)
|
|
|
|
}
|
2017-02-14 04:54:11 +01:00
|
|
|
}
|
|
|
|
|
2020-10-12 14:52:53 +02:00
|
|
|
// Serve the actual basic authentication middleware.
|
|
|
|
// Use the Context.User method to retrieve the stored user.
|
2020-07-10 22:21:09 +02:00
|
|
|
func (b *basicAuthMiddleware) Serve(ctx *context.Context) {
|
2017-06-10 14:28:09 +02:00
|
|
|
auth, found := b.findAuth(ctx.GetHeader("Authorization"))
|
2020-10-12 01:07:04 +02:00
|
|
|
if !found || auth.forceLogout {
|
|
|
|
if auth != nil {
|
|
|
|
auth.mu.Lock()
|
|
|
|
auth.forceLogout = false
|
|
|
|
auth.mu.Unlock()
|
|
|
|
}
|
|
|
|
|
2017-02-14 04:54:11 +01:00
|
|
|
b.askForCredentials(ctx)
|
2017-10-26 01:48:16 +02:00
|
|
|
ctx.StopExecution()
|
2017-06-10 14:28:09 +02:00
|
|
|
return
|
2017-02-14 04:54:11 +01:00
|
|
|
// don't continue to the next handler
|
2017-06-10 14:28:09 +02:00
|
|
|
}
|
2020-10-12 01:07:04 +02:00
|
|
|
|
2020-10-12 14:52:53 +02:00
|
|
|
auth.mu.RLock()
|
|
|
|
logged := auth.logged
|
|
|
|
auth.mu.RUnlock()
|
|
|
|
if !logged {
|
|
|
|
auth.mu.Lock()
|
|
|
|
auth.authorizedAt = time.Now()
|
|
|
|
auth.mu.Unlock()
|
|
|
|
}
|
|
|
|
|
2017-06-10 14:28:09 +02:00
|
|
|
// all ok
|
|
|
|
if b.expireEnabled {
|
2020-10-12 14:52:53 +02:00
|
|
|
if !logged {
|
2020-08-15 14:40:41 +02:00
|
|
|
auth.mu.Lock()
|
2020-10-12 14:52:53 +02:00
|
|
|
auth.expires = auth.authorizedAt.Add(b.config.Expires)
|
2017-06-10 14:28:09 +02:00
|
|
|
auth.logged = true
|
2020-08-15 14:40:41 +02:00
|
|
|
auth.mu.Unlock()
|
2017-06-10 14:28:09 +02:00
|
|
|
}
|
2017-02-14 04:54:11 +01:00
|
|
|
|
2020-08-15 14:40:41 +02:00
|
|
|
auth.mu.RLock()
|
|
|
|
expired := time.Now().After(auth.expires)
|
|
|
|
auth.mu.RUnlock()
|
|
|
|
if expired {
|
|
|
|
auth.mu.Lock()
|
|
|
|
auth.logged = false
|
2020-10-12 14:52:53 +02:00
|
|
|
auth.forceLogout = false
|
2020-08-15 14:40:41 +02:00
|
|
|
auth.mu.Unlock()
|
2017-06-10 14:28:09 +02:00
|
|
|
b.askForCredentials(ctx) // ask for authentication again
|
2017-10-26 01:48:16 +02:00
|
|
|
ctx.StopExecution()
|
2017-06-10 14:28:09 +02:00
|
|
|
return
|
2017-02-14 04:54:11 +01:00
|
|
|
}
|
|
|
|
}
|
2020-10-12 01:07:04 +02:00
|
|
|
|
2020-10-12 14:52:53 +02:00
|
|
|
if !b.config.DisableContextUser {
|
2020-10-12 01:07:04 +02:00
|
|
|
ctx.SetLogoutFunc(b.Logout)
|
2020-10-12 14:52:53 +02:00
|
|
|
|
|
|
|
auth.mu.RLock()
|
|
|
|
user := &context.SimpleUser{
|
|
|
|
Authorization: authorizationType,
|
|
|
|
AuthorizedAt: auth.authorizedAt,
|
|
|
|
Username: auth.Username,
|
|
|
|
Password: auth.Password,
|
|
|
|
}
|
|
|
|
auth.mu.RUnlock()
|
|
|
|
ctx.SetUser(user)
|
2020-10-12 01:07:04 +02:00
|
|
|
}
|
|
|
|
|
2017-06-10 14:28:09 +02:00
|
|
|
ctx.Next() // continue
|
2017-02-14 04:54:11 +01:00
|
|
|
}
|
2020-10-12 01:07:04 +02:00
|
|
|
|
|
|
|
// Logout sends a 401 so the browser/client can invalidate the
|
|
|
|
// Basic Authentication and also sets the underline user's logged field to false,
|
|
|
|
// so its expiration resets when re-ask for credentials.
|
|
|
|
//
|
|
|
|
// End-developers should call the `Context.Logout()` method
|
|
|
|
// to fire this method as this structure is hidden.
|
|
|
|
func (b *basicAuthMiddleware) Logout(ctx *context.Context) {
|
|
|
|
ctx.StatusCode(401)
|
|
|
|
if auth, found := b.findAuth(ctx.GetHeader("Authorization")); found {
|
|
|
|
auth.mu.Lock()
|
|
|
|
auth.logged = false
|
|
|
|
auth.forceLogout = true
|
|
|
|
auth.mu.Unlock()
|
|
|
|
}
|
|
|
|
}
|