1
0
mirror of https://github.com/kataras/iris.git synced 2025-12-17 09:57:01 +00:00

add authenticated-controller example - proof of concept

Former-commit-id: 7a15da2bd966f2a699337cd494af8b5d1877b11d
This commit is contained in:
Gerasimos (Makis) Maropoulos
2020-05-06 04:45:12 +03:00
parent 47075a82e9
commit ffc493a0b9
9 changed files with 200 additions and 50 deletions

View File

@@ -1,6 +1,7 @@
package sessions
import (
"math"
"reflect"
"strconv"
"sync"
@@ -225,20 +226,22 @@ func newErrEntryNotFound(key string, kind reflect.Kind, value interface{}) *ErrE
func (s *Session) GetInt(key string) (int, error) {
v := s.Get(key)
if vint, ok := v.(int); ok {
return vint, nil
}
if v != nil {
if vint, ok := v.(int); ok {
return vint, nil
}
if vfloat64, ok := v.(float64); ok {
return int(vfloat64), nil
}
if vfloat64, ok := v.(float64); ok {
return int(vfloat64), nil
}
if vint64, ok := v.(int64); ok {
return int(vint64), nil
}
if vint64, ok := v.(int64); ok {
return int(vint64), nil
}
if vstring, sok := v.(string); sok {
return strconv.Atoi(vstring)
if vstring, sok := v.(string); sok {
return strconv.Atoi(vstring)
}
}
return -1, newErrEntryNotFound(key, reflect.Int, v)
@@ -277,21 +280,22 @@ func (s *Session) Decrement(key string, n int) (newValue int) {
// if key doesn't exist then it returns -1 and a non-nil error.
func (s *Session) GetInt64(key string) (int64, error) {
v := s.Get(key)
if v != nil {
if vint64, ok := v.(int64); ok {
return vint64, nil
}
if vint64, ok := v.(int64); ok {
return vint64, nil
}
if vfloat64, ok := v.(float64); ok {
return int64(vfloat64), nil
}
if vfloat64, ok := v.(float64); ok {
return int64(vfloat64), nil
}
if vint, ok := v.(int); ok {
return int64(vint), nil
}
if vint, ok := v.(int); ok {
return int64(vint), nil
}
if vstring, sok := v.(string); sok {
return strconv.ParseInt(vstring, 10, 64)
if vstring, sok := v.(string); sok {
return strconv.ParseInt(vstring, 10, 64)
}
}
return -1, newErrEntryNotFound(key, reflect.Int64, v)
@@ -307,6 +311,49 @@ func (s *Session) GetInt64Default(key string, defaultValue int64) int64 {
return defaultValue
}
// GetUint64 same as `Get` but returns as uint64,
// if key doesn't exist then it returns 0 and a non-nil error.
func (s *Session) GetUint64(key string) (uint64, error) {
v := s.Get(key)
if v != nil {
switch vv := v.(type) {
case string:
val, err := strconv.ParseUint(vv, 10, 64)
if err != nil {
return 0, err
}
if val > math.MaxUint64 {
break
}
return uint64(val), nil
case uint8:
return uint64(vv), nil
case uint16:
return uint64(vv), nil
case uint32:
return uint64(vv), nil
case uint64:
return vv, nil
case int64:
return uint64(vv), nil
case int:
return uint64(vv), nil
}
}
return 0, newErrEntryNotFound(key, reflect.Uint64, v)
}
// GetUint64Default same as `Get` but returns as uint64,
// if key doesn't exist it returns the "defaultValue".
func (s *Session) GetUint64Default(key string, defaultValue uint64) uint64 {
if v, err := s.GetUint64(key); err == nil {
return v
}
return defaultValue
}
// GetFloat32 same as `Get` but returns its float32 representation,
// if key doesn't exist then it returns -1 and a non-nil error.
func (s *Session) GetFloat32(key string) (float32, error) {

View File

@@ -7,6 +7,10 @@ import (
"github.com/kataras/iris/v12/context"
)
func init() {
context.SetHandlerName("iris/sessions.*Handler", "iris.session")
}
// A Sessions manager should be responsible to Start a sesion, based
// on a Context, which should return
// a compatible Session interface, type. If the external session manager