-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: move auth logic into middleware && naming the files better (#115)
* chore: move auth logic into middleware * refactor: move the project ownership into central place * refactor: give files better names
- Loading branch information
Showing
7 changed files
with
132 additions
and
90 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package api | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"net/http" | ||
|
||
"github.com/julienschmidt/httprouter" | ||
"github.com/rakutentech/shibuya/shibuya/model" | ||
) | ||
|
||
const ( | ||
accountKey = "account" | ||
) | ||
|
||
func authWithSession(r *http.Request) (*model.Account, error) { | ||
account := model.GetAccountBySession(r) | ||
if account == nil { | ||
return nil, makeLoginError() | ||
} | ||
return account, nil | ||
} | ||
|
||
// TODO add JWT token auth in the future | ||
func authWithToken(_ *http.Request) (*model.Account, error) { | ||
return nil, errors.New("No token presented") | ||
} | ||
|
||
func (s *ShibuyaAPI) authRequired(next httprouter.Handle) httprouter.Handle { | ||
return httprouter.Handle(func(w http.ResponseWriter, r *http.Request, params httprouter.Params) { | ||
var account *model.Account | ||
var err error | ||
account, err = authWithSession(r) | ||
if err != nil { | ||
s.handleErrors(w, err) | ||
return | ||
} | ||
next(w, r.WithContext(context.WithValue(r.Context(), accountKey, account)), params) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package api | ||
|
||
import ( | ||
"net/http" | ||
"strings" | ||
) | ||
|
||
func retrieveClientIP(r *http.Request) string { | ||
t := r.Header.Get("x-forwarded-for") | ||
if t == "" { | ||
return r.RemoteAddr | ||
} | ||
return strings.Split(t, ",")[0] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package api | ||
|
||
import ( | ||
"net/http" | ||
|
||
"github.com/julienschmidt/httprouter" | ||
"github.com/rakutentech/shibuya/shibuya/model" | ||
) | ||
|
||
func hasProjectOwnership(project *model.Project, account *model.Account) bool { | ||
if _, ok := account.MLMap[project.Owner]; !ok { | ||
if !account.IsAdmin() { | ||
return false | ||
} | ||
} | ||
return true | ||
} | ||
|
||
func hasCollectionOwnership(r *http.Request, params httprouter.Params) (*model.Collection, error) { | ||
collection, err := getCollection(params.ByName("collection_id")) | ||
if err != nil { | ||
return nil, err | ||
} | ||
account := r.Context().Value(accountKey).(*model.Account) | ||
project, err := model.GetProject(collection.ProjectID) | ||
if err != nil { | ||
return nil, err | ||
} | ||
if r := hasProjectOwnership(project, account); !r { | ||
return nil, makeCollectionOwnershipError() | ||
} | ||
return collection, nil | ||
} |
Oops, something went wrong.