-
Notifications
You must be signed in to change notification settings - Fork 48
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
SDK selection refactor #197
Merged
+2,491
−2,992
Merged
Changes from 7 commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
d4fd6ce
refactor WalletService, wallet errors
lyoshenka 0047710
get rid of ghetto mock rpc server in tests so we can stop using Calle…
lyoshenka d262891
drop proxy Client, merge it into Caller
lyoshenka ac319d6
straighten out error codes
lyoshenka 79a4d07
slow progress
lyoshenka 8cefa94
tests pass now
lyoshenka 4836971
clarify what it means to send empty string to chan
lyoshenka cbd6068
fixed a bunch more things with auth. finally dropped sdkrouter.GetSer…
lyoshenka d46134d
add tests for recovery handler
lyoshenka 2d4c559
minor
lyoshenka b8af1e9
add user info to /internal/status
lyoshenka 797d024
added trace logging for sdkrouter locks
lyoshenka 84157a6
some fixes
lyoshenka 3b842f8
drop models from coveralls
lyoshenka ebd1b78
tests for test.AssertJsonEqual
lyoshenka bf40725
deduping
lyoshenka 026a8f7
only init sentry once
lyoshenka 601d8e2
save wallet IDs in db for BC. revert this later
lyoshenka File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 was deleted.
Oops, something went wrong.
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,65 @@ | ||
package auth | ||
|
||
import ( | ||
"context" | ||
"net/http" | ||
|
||
"github.com/lbryio/lbrytv/app/sdkrouter" | ||
"github.com/lbryio/lbrytv/app/wallet" | ||
"github.com/lbryio/lbrytv/internal/ip" | ||
"github.com/lbryio/lbrytv/internal/monitor" | ||
"github.com/lbryio/lbrytv/models" | ||
|
||
"github.com/gorilla/mux" | ||
) | ||
|
||
var logger = monitor.NewModuleLogger("auth") | ||
|
||
const ContextKey = "user" | ||
|
||
type Result struct { | ||
User *models.User | ||
Err error | ||
} | ||
|
||
func (r *Result) AuthAttempted() bool { return r.User != nil || r.Err != nil } | ||
func (r *Result) AuthFailed() bool { return r.Err != nil } | ||
func (r *Result) Authenticated() bool { return r.User != nil } | ||
|
||
func FromRequest(r *http.Request) *Result { | ||
v := r.Context().Value(ContextKey) | ||
if v == nil { | ||
panic("Auth middleware was not applied") | ||
lyoshenka marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
return v.(*Result) | ||
} | ||
|
||
// Retriever gets a user by hitting internal-api with the provided auth token | ||
// and matching the response to a local user. | ||
// NOTE: The retrieved user must come with a wallet that's created and ready to use. | ||
type Retriever func(token, metaRemoteIP string) (*models.User, error) | ||
|
||
func AllInOneRetrieverThatNeedsRefactoring(rt *sdkrouter.Router, internalAPIHost string) Retriever { | ||
return func(token, metaRemoteIP string) (user *models.User, err error) { | ||
return wallet.GetUserWithWallet(rt, internalAPIHost, token, metaRemoteIP) | ||
} | ||
} | ||
|
||
func Middleware(retriever Retriever) mux.MiddlewareFunc { | ||
return func(next http.Handler) http.Handler { | ||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
ar := &Result{} | ||
if token, ok := r.Header[wallet.TokenHeader]; ok { | ||
addr := ip.AddressForRequest(r) | ||
user, err := retriever(token[0], addr) | ||
if err != nil { | ||
logger.LogF(monitor.F{"ip": addr}).Debugf("failed to authenticate user") | ||
ar.Err = err | ||
} else { | ||
ar.User = user | ||
} | ||
} | ||
next.ServeHTTP(w, r.Clone(context.WithValue(r.Context(), ContextKey, ar))) | ||
}) | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe it's just me, but I like to keep any views logic, no matter how trivial, in the
handlers
source file to minimize surprises in code discovery.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what do you mean surprises in code discovery?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The kind of surprise when you read through
handlers.go
and realize that some of the handlers logic is actually inroutes.go