Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
B
beego
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
go
beego
Commits
d8b9db8d
Commit
d8b9db8d
authored
Feb 22, 2014
by
slene
Committed by
asta.xie
Mar 12, 2014
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
move SetSecureCookie / GetSecureCookie to *context.Context and alias in Controller
parent
9b498fea
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
48 additions
and
36 deletions
+48
-36
context.go
context/context.go
+45
-0
controller.go
controller.go
+3
-36
No files found.
context/context.go
View file @
d8b9db8d
package
context
package
context
import
(
import
(
"crypto/hmac"
"crypto/sha1"
"encoding/base64"
"fmt"
"net/http"
"net/http"
"strconv"
"strings"
"time"
"github.com/astaxie/beego/middleware"
"github.com/astaxie/beego/middleware"
)
)
...
@@ -59,3 +66,41 @@ func (ctx *Context) GetCookie(key string) string {
...
@@ -59,3 +66,41 @@ func (ctx *Context) GetCookie(key string) string {
func
(
ctx
*
Context
)
SetCookie
(
name
string
,
value
string
,
others
...
interface
{})
{
func
(
ctx
*
Context
)
SetCookie
(
name
string
,
value
string
,
others
...
interface
{})
{
ctx
.
Output
.
Cookie
(
name
,
value
,
others
...
)
ctx
.
Output
.
Cookie
(
name
,
value
,
others
...
)
}
}
// Get secure cookie from request by a given key.
func
(
ctx
*
Context
)
GetSecureCookie
(
Secret
,
key
string
)
(
string
,
bool
)
{
val
:=
ctx
.
Input
.
Cookie
(
key
)
if
val
==
""
{
return
""
,
false
}
parts
:=
strings
.
SplitN
(
val
,
"|"
,
3
)
if
len
(
parts
)
!=
3
{
return
""
,
false
}
vs
:=
parts
[
0
]
timestamp
:=
parts
[
1
]
sig
:=
parts
[
2
]
h
:=
hmac
.
New
(
sha1
.
New
,
[]
byte
(
Secret
))
fmt
.
Fprintf
(
h
,
"%s%s"
,
vs
,
timestamp
)
if
fmt
.
Sprintf
(
"%02x"
,
h
.
Sum
(
nil
))
!=
sig
{
return
""
,
false
}
res
,
_
:=
base64
.
URLEncoding
.
DecodeString
(
vs
)
return
string
(
res
),
true
}
// Set Secure cookie for response.
func
(
ctx
*
Context
)
SetSecureCookie
(
Secret
,
name
,
value
string
,
others
...
interface
{})
{
vs
:=
base64
.
URLEncoding
.
EncodeToString
([]
byte
(
value
))
timestamp
:=
strconv
.
FormatInt
(
time
.
Now
()
.
UnixNano
(),
10
)
h
:=
hmac
.
New
(
sha1
.
New
,
[]
byte
(
Secret
))
fmt
.
Fprintf
(
h
,
"%s%s"
,
vs
,
timestamp
)
sig
:=
fmt
.
Sprintf
(
"%02x"
,
h
.
Sum
(
nil
))
cookie
:=
strings
.
Join
([]
string
{
vs
,
timestamp
,
sig
},
"|"
)
ctx
.
Output
.
Cookie
(
name
,
cookie
,
others
...
)
}
controller.go
View file @
d8b9db8d
...
@@ -2,11 +2,7 @@ package beego
...
@@ -2,11 +2,7 @@ package beego
import
(
import
(
"bytes"
"bytes"
"crypto/hmac"
"crypto/sha1"
"encoding/base64"
"errors"
"errors"
"fmt"
"html/template"
"html/template"
"io"
"io"
"io/ioutil"
"io/ioutil"
...
@@ -17,7 +13,6 @@ import (
...
@@ -17,7 +13,6 @@ import (
"reflect"
"reflect"
"strconv"
"strconv"
"strings"
"strings"
"time"
"github.com/astaxie/beego/context"
"github.com/astaxie/beego/context"
"github.com/astaxie/beego/session"
"github.com/astaxie/beego/session"
...
@@ -417,40 +412,12 @@ func (c *Controller) IsAjax() bool {
...
@@ -417,40 +412,12 @@ func (c *Controller) IsAjax() bool {
// GetSecureCookie returns decoded cookie value from encoded browser cookie values.
// GetSecureCookie returns decoded cookie value from encoded browser cookie values.
func
(
c
*
Controller
)
GetSecureCookie
(
Secret
,
key
string
)
(
string
,
bool
)
{
func
(
c
*
Controller
)
GetSecureCookie
(
Secret
,
key
string
)
(
string
,
bool
)
{
val
:=
c
.
Ctx
.
GetCookie
(
key
)
return
c
.
Ctx
.
GetSecureCookie
(
Secret
,
key
)
if
val
==
""
{
return
""
,
false
}
parts
:=
strings
.
SplitN
(
val
,
"|"
,
3
)
if
len
(
parts
)
!=
3
{
return
""
,
false
}
vs
:=
parts
[
0
]
timestamp
:=
parts
[
1
]
sig
:=
parts
[
2
]
h
:=
hmac
.
New
(
sha1
.
New
,
[]
byte
(
Secret
))
fmt
.
Fprintf
(
h
,
"%s%s"
,
vs
,
timestamp
)
if
fmt
.
Sprintf
(
"%02x"
,
h
.
Sum
(
nil
))
!=
sig
{
return
""
,
false
}
res
,
_
:=
base64
.
URLEncoding
.
DecodeString
(
vs
)
return
string
(
res
),
true
}
}
// SetSecureCookie puts value into cookie after encoded the value.
// SetSecureCookie puts value into cookie after encoded the value.
func
(
c
*
Controller
)
SetSecureCookie
(
Secret
,
name
,
val
string
,
age
int64
)
{
func
(
c
*
Controller
)
SetSecureCookie
(
Secret
,
name
,
value
string
,
others
...
interface
{})
{
vs
:=
base64
.
URLEncoding
.
EncodeToString
([]
byte
(
val
))
c
.
Ctx
.
SetSecureCookie
(
Secret
,
name
,
value
,
others
...
)
timestamp
:=
strconv
.
FormatInt
(
time
.
Now
()
.
UnixNano
(),
10
)
h
:=
hmac
.
New
(
sha1
.
New
,
[]
byte
(
Secret
))
fmt
.
Fprintf
(
h
,
"%s%s"
,
vs
,
timestamp
)
sig
:=
fmt
.
Sprintf
(
"%02x"
,
h
.
Sum
(
nil
))
cookie
:=
strings
.
Join
([]
string
{
vs
,
timestamp
,
sig
},
"|"
)
c
.
Ctx
.
SetCookie
(
name
,
cookie
,
age
,
"/"
)
}
}
// XsrfToken creates a xsrf token string and returns.
// XsrfToken creates a xsrf token string and returns.
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment