Commit b92e6515 authored by zauberstuhl's avatar zauberstuhl

Retrieve access_token from HTTP headers

parent 75c35326
......@@ -50,12 +50,10 @@ func redirectIfLoggedIn(c *revel.Controller) revel.Result {
}
func requiresTokenLogin(c *revel.Controller) revel.Result {
var accessToken string
c.Params.Bind(&accessToken, "access_token")
if accessToken != "" {
accessToken := c.Request.Header.Server.Get("access_token")
if len(accessToken) > 0 {
var token models.OAuthToken
err := token.FindByToken(accessToken)
err := token.FindByToken(accessToken[0])
if err != nil {
c.Response.Status = http.StatusUnauthorized
c.Log.Error(api.ERR_UNAUTHORIZED, "err", err)
......
......@@ -29,7 +29,7 @@ type Post struct {
func (p Post) Index(guid string) revel.Result {
var post models.Post
user, err := models.CurrentUser(p.Params, p.Session)
user, err := models.CurrentUser(p.Controller)
if err == nil {
p.ViewArgs["currentUser"] = user
}
......
......@@ -43,7 +43,7 @@ func (p Profile) IndexPagination(guid string, page int) revel.Result {
return p.RenderError(err)
}
user, err := models.CurrentUser(p.Params, p.Session)
user, err := models.CurrentUser(p.Controller)
if err == nil {
p.ViewArgs["currentUser"] = user
}
......
......@@ -34,7 +34,7 @@ func (s PublicStream) IndexPagination(page int) revel.Result {
var posts models.Posts
var offset int = ((page - 1) * 10)
user, err := models.CurrentUser(s.Params, s.Session)
user, err := models.CurrentUser(s.Controller)
if err == nil {
s.ViewArgs["currentUser"] = user
}
......
......@@ -37,7 +37,7 @@ func (s Search) IndexPagination(text string, page int) revel.Result {
var offset int = ((page - 1) * 10)
text = strings.Replace(text, "'", "", -1)
user, err := models.CurrentUser(s.Params, s.Session)
user, err := models.CurrentUser(s.Controller)
if err != nil {
s.Log.Error("Cannot fetch current user", "error", err)
return s.RenderError(err)
......
......@@ -27,7 +27,7 @@ type Setting struct {
}
func (s Setting) Index() revel.Result {
user, err := models.CurrentUser(s.Params, s.Session)
user, err := models.CurrentUser(s.Controller)
if err != nil {
s.Log.Error("Cannot fetch current user", "error", err)
return s.RenderError(err)
......
......@@ -34,7 +34,7 @@ func (s Stream) IndexPagination(page int) revel.Result {
var posts models.Posts
var offset int = ((page - 1) * 10)
user, err := models.CurrentUser(s.Params, s.Session)
user, err := models.CurrentUser(s.Controller)
if err != nil {
s.Log.Error("Cannot fetch current user", "error", err)
return s.RenderError(err)
......
......@@ -33,7 +33,7 @@ func (t Tag) Index(name string) revel.Result {
tag models.Tag
)
user, err := models.CurrentUser(t.Params, t.Session)
user, err := models.CurrentUser(t.Controller)
if err == nil {
t.ViewArgs["currentUser"] = user
}
......
......@@ -62,7 +62,7 @@ func OpenDatabase() (*gorm.DB, error) {
// like described here https://revel.github.io/manual/controllers.html
// but it will throw me always:
// panic: NewRoute: Failed to find controller for route path action
func CurrentUser(p *revel.Params, s revel.Session) (User, error) {
func CurrentUser(c *revel.Controller) (User, error) {
db, err := OpenDatabase()
if err != nil {
revel.WARN.Println(err)
......@@ -70,11 +70,10 @@ func CurrentUser(p *revel.Params, s revel.Session) (User, error) {
}
defer db.Close()
var accessToken string
p.Bind(&accessToken, "access_token")
if accessToken != "" {
accessToken := c.Request.Header.Server.Get("access_token")
if len(accessToken) > 0 {
var token OAuthToken
err := token.FindByToken(accessToken)
err := token.FindByToken(accessToken[0])
if err != nil {
revel.AppLog.Error("Cannot find token", "error", err)
return User{}, err
......@@ -83,7 +82,7 @@ func CurrentUser(p *revel.Params, s revel.Session) (User, error) {
}
var session Session
err = db.Where("token = ?", s["TOKEN"]).First(&session).Error
err = db.Where("token = ?", c.Session["TOKEN"]).First(&session).Error
if err != nil {
revel.ERROR.Println(err)
return User{}, err
......@@ -192,14 +191,12 @@ func parentIsLocal(postID uint) (user User, found bool) {
if err != nil {
return
}
db.Model(&post).Related(&post.Person, "Person")
if post.Person.UserID > 0 {
err = db.First(&user, post.Person.UserID).Error
if err != nil {
return
}
db.Model(&user).Related(&user.Person, "Person")
found = true
return
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment