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