2021-12-25 02:16:58 +01:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"net/http"
|
2022-06-17 22:12:20 +02:00
|
|
|
"net/mail"
|
2021-12-25 02:16:58 +01:00
|
|
|
"orblivion/lbry-id/auth"
|
|
|
|
"orblivion/lbry-id/store"
|
|
|
|
)
|
|
|
|
|
|
|
|
// TODO proper doc comments!
|
|
|
|
|
2022-06-17 21:38:44 +02:00
|
|
|
const ApiVersion = "1"
|
|
|
|
const PathPrefix = "/api/" + ApiVersion
|
2022-06-16 23:58:11 +02:00
|
|
|
|
2022-06-17 21:38:44 +02:00
|
|
|
const PathAuthToken = PathPrefix + "/auth/full"
|
|
|
|
const PathRegister = PathPrefix + "/signup"
|
|
|
|
const PathWallet = PathPrefix + "/wallet"
|
2021-12-25 02:16:58 +01:00
|
|
|
|
|
|
|
type Server struct {
|
2022-06-09 23:04:49 +02:00
|
|
|
auth auth.AuthInterface
|
|
|
|
store store.StoreInterface
|
2021-12-25 02:16:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func Init(
|
|
|
|
auth auth.AuthInterface,
|
|
|
|
store store.StoreInterface,
|
|
|
|
) *Server {
|
|
|
|
return &Server{
|
2022-06-09 23:04:49 +02:00
|
|
|
auth: auth,
|
|
|
|
store: store,
|
2021-12-25 02:16:58 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type ErrorResponse struct {
|
|
|
|
Error string `json:"error"`
|
|
|
|
}
|
|
|
|
|
2022-06-07 19:25:14 +02:00
|
|
|
func errorJson(w http.ResponseWriter, code int, extra string) {
|
2021-12-25 02:16:58 +01:00
|
|
|
errorStr := http.StatusText(code)
|
|
|
|
if extra != "" {
|
|
|
|
errorStr = errorStr + ": " + extra
|
|
|
|
}
|
2022-06-07 19:25:14 +02:00
|
|
|
authErrorJson, err := json.Marshal(ErrorResponse{Error: errorStr})
|
2021-12-25 02:16:58 +01:00
|
|
|
if err != nil {
|
|
|
|
// In case something really stupid happens
|
|
|
|
http.Error(w, `{"error": "error when JSON-encoding error message"}`, code)
|
|
|
|
}
|
2022-06-07 19:25:14 +02:00
|
|
|
http.Error(w, string(authErrorJson), code)
|
2021-12-25 02:16:58 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Don't report any details to the user. Log it instead.
|
2022-06-07 19:25:14 +02:00
|
|
|
func internalServiceErrorJson(w http.ResponseWriter, serverErr error, errContext string) {
|
2021-12-25 02:16:58 +01:00
|
|
|
errorStr := http.StatusText(http.StatusInternalServerError)
|
2022-06-07 19:25:14 +02:00
|
|
|
authErrorJson, err := json.Marshal(ErrorResponse{Error: errorStr})
|
2021-12-25 02:16:58 +01:00
|
|
|
if err != nil {
|
|
|
|
// In case something really stupid happens
|
|
|
|
http.Error(w, `{"error": "error when JSON-encoding error message"}`, http.StatusInternalServerError)
|
2022-06-07 19:25:14 +02:00
|
|
|
log.Printf("error when JSON-encoding error message")
|
|
|
|
return
|
2021-12-25 02:16:58 +01:00
|
|
|
}
|
2022-06-07 19:25:14 +02:00
|
|
|
http.Error(w, string(authErrorJson), http.StatusInternalServerError)
|
|
|
|
log.Printf("%s: %+v\n", errContext, serverErr)
|
2021-12-25 02:16:58 +01:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
//////////////////
|
|
|
|
// Handler Helpers
|
|
|
|
//////////////////
|
|
|
|
|
|
|
|
// Cut down on code repetition. No need to return errors since it can all be
|
|
|
|
// handled here. Just return a bool to indicate success.
|
|
|
|
// TODO the names `getPostData` and `getGetData` don't fully describe what they do
|
|
|
|
|
|
|
|
func requestOverhead(w http.ResponseWriter, req *http.Request, method string) bool {
|
|
|
|
if req.Method != method {
|
2022-06-07 19:25:14 +02:00
|
|
|
errorJson(w, http.StatusMethodNotAllowed, "")
|
2021-12-25 02:16:58 +01:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// All structs representing incoming json request body should implement this
|
|
|
|
type PostRequest interface {
|
|
|
|
validate() bool
|
|
|
|
}
|
|
|
|
|
2022-06-19 23:49:05 +02:00
|
|
|
// TODO decoder.DisallowUnknownFields?
|
|
|
|
// TODO GET params too large (like StatusRequestEntityTooLarge)? Or is that
|
|
|
|
// somehow handled by the http library due to a size limit in the http spec?
|
|
|
|
|
2021-12-25 02:16:58 +01:00
|
|
|
// Confirm it's a Post request, various overhead, decode the json, validate the struct
|
|
|
|
func getPostData(w http.ResponseWriter, req *http.Request, reqStruct PostRequest) bool {
|
|
|
|
if !requestOverhead(w, req, http.MethodPost) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2022-06-19 23:49:05 +02:00
|
|
|
// Make the limit 100k. Increase from there as needed. I'd rather block some
|
|
|
|
// people's large wallets and increase the limit than OOM for everybody and
|
|
|
|
// decrease the limit.
|
|
|
|
req.Body = http.MaxBytesReader(w, req.Body, 100000)
|
|
|
|
err := json.NewDecoder(req.Body).Decode(&reqStruct)
|
|
|
|
switch {
|
|
|
|
case err == nil:
|
|
|
|
break
|
|
|
|
case err.Error() == "http: request body too large":
|
|
|
|
errorJson(w, http.StatusRequestEntityTooLarge, "")
|
|
|
|
return false
|
|
|
|
default:
|
|
|
|
// Maybe we can suss out specific errors later. Need to study what errors
|
|
|
|
// come from Decode.
|
|
|
|
errorJson(w, http.StatusBadRequest, "Error parsing JSON")
|
2021-12-25 02:16:58 +01:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if !reqStruct.validate() {
|
|
|
|
// TODO validate() should return useful error messages instead of a bool.
|
2022-06-07 19:25:14 +02:00
|
|
|
errorJson(w, http.StatusBadRequest, "Request failed validation")
|
2021-12-25 02:16:58 +01:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// Confirm it's a Get request, various overhead
|
|
|
|
func getGetData(w http.ResponseWriter, req *http.Request) bool {
|
|
|
|
return requestOverhead(w, req, http.MethodGet)
|
|
|
|
}
|
|
|
|
|
2022-06-07 19:25:14 +02:00
|
|
|
// TODO - probably don't return all of authToken since we only need userId and
|
|
|
|
// deviceId. Also this is apparently not idiomatic go error handling.
|
2021-12-25 02:16:58 +01:00
|
|
|
func (s *Server) checkAuth(
|
|
|
|
w http.ResponseWriter,
|
2022-06-09 23:04:49 +02:00
|
|
|
token auth.TokenString,
|
2021-12-25 02:16:58 +01:00
|
|
|
scope auth.AuthScope,
|
2022-06-07 19:25:14 +02:00
|
|
|
) *auth.AuthToken {
|
|
|
|
authToken, err := s.store.GetToken(token)
|
2021-12-25 02:16:58 +01:00
|
|
|
if err == store.ErrNoToken {
|
2022-06-07 19:25:14 +02:00
|
|
|
errorJson(w, http.StatusUnauthorized, "Token Not Found")
|
|
|
|
return nil
|
2021-12-25 02:16:58 +01:00
|
|
|
}
|
|
|
|
if err != nil {
|
2022-06-07 19:25:14 +02:00
|
|
|
internalServiceErrorJson(w, err, "Error getting Token")
|
|
|
|
return nil
|
2021-12-25 02:16:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if !authToken.ScopeValid(scope) {
|
2022-06-07 19:25:14 +02:00
|
|
|
errorJson(w, http.StatusForbidden, "Scope")
|
|
|
|
return nil
|
2021-12-25 02:16:58 +01:00
|
|
|
}
|
|
|
|
|
2022-06-07 19:25:14 +02:00
|
|
|
return authToken
|
2021-12-25 02:16:58 +01:00
|
|
|
}
|
|
|
|
|
2022-06-17 22:12:20 +02:00
|
|
|
func validateEmail(email auth.Email) bool {
|
|
|
|
e, err := mail.ParseAddress(string(email))
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
// "Joe <joe@example.com>" is valid according to ParseAddress. Likewise
|
|
|
|
// " joe@example.com". Etc. We only want the exact address, "joe@example.com"
|
|
|
|
// to be valid. ParseAddress will extract the exact address as e.Address. So
|
|
|
|
// we'll take the input email, put it through ParseAddress, see if it parses
|
|
|
|
// successfully, and then compare the input email to e.Address to make sure
|
|
|
|
// that it was an exact address to begin with.
|
|
|
|
return string(email) == e.Address
|
|
|
|
}
|
|
|
|
|
2021-12-25 02:16:58 +01:00
|
|
|
// TODO - both wallet and token requests should be PUT, not POST.
|
|
|
|
// PUT = "...creates a new resource or replaces a representation of the target resource with the request payload."
|
|
|
|
|
|
|
|
func (s *Server) Serve() {
|
2022-06-17 21:38:44 +02:00
|
|
|
http.HandleFunc(PathAuthToken, s.getAuthToken)
|
|
|
|
http.HandleFunc(PathWallet, s.handleWallet)
|
|
|
|
http.HandleFunc(PathRegister, s.register)
|
2021-12-25 02:16:58 +01:00
|
|
|
|
2022-06-16 16:01:49 +02:00
|
|
|
fmt.Println("Serving at localhost:8090")
|
|
|
|
http.ListenAndServe("localhost:8090", nil)
|
2021-12-25 02:16:58 +01:00
|
|
|
}
|