-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from visiperf/feat/vercel-serverless-function
Feat/vercel serverless function
- Loading branch information
Showing
14 changed files
with
330 additions
and
14 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,4 +3,5 @@ | |
.idea/ | ||
|
||
# environment | ||
.env | ||
.env | ||
.vercel |
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,37 @@ | ||
package api | ||
|
||
import ( | ||
"net/http" | ||
"net/url" | ||
|
||
"github.com/visiperf/visiauth/v3" | ||
"github.com/visiperf/visiauth/v3/api/renderer" | ||
"github.com/visiperf/visiauth/v3/errors" | ||
"github.com/visiperf/visiauth/v3/neo4j" | ||
"github.com/visiperf/visiauth/v3/redis" | ||
) | ||
|
||
const ( | ||
accessTokenQueryParamsKey = "token" | ||
) | ||
|
||
func MeHandler(w http.ResponseWriter, r *http.Request) { | ||
authenticable, err := func() (visiauth.Authenticable, error) { | ||
vs, err := url.ParseQuery(r.URL.RawQuery) | ||
if err != nil { | ||
return nil, errors.InvalidArgument(err.Error(), "INVALID_QUERY_PARAMS") | ||
} | ||
|
||
if len(vs.Get(accessTokenQueryParamsKey)) <= 0 { | ||
return nil, errors.InvalidArgument("token in query params is required", "TOKEN_QUERY_PARAMS_REQUIRED") | ||
} | ||
|
||
return visiauth.NewService(redis.NewJwkFetcher(), neo4j.NewUserRepository()).DecodeAccessToken(r.Context(), vs.Get(accessTokenQueryParamsKey)) | ||
}() | ||
if err != nil { | ||
renderer.Error(err, w) | ||
return | ||
} | ||
|
||
renderer.Success(authenticable, http.StatusOK, w) | ||
} |
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,39 @@ | ||
package renderer | ||
|
||
import ( | ||
"encoding/json" | ||
"errors" | ||
"net/http" | ||
) | ||
|
||
func Success(v interface{}, statusCode int, rw http.ResponseWriter) { | ||
data, err := json.Marshal(map[string]interface{}{"data": v}) | ||
if err != nil { | ||
Error(err, rw) | ||
return | ||
} | ||
|
||
render(data, statusCode, "application/json", rw) | ||
} | ||
|
||
func Error(err error, rw http.ResponseWriter) { | ||
data, _ := json.Marshal(map[string]interface{}{"error": err}) | ||
|
||
var sce interface { | ||
error | ||
StatusCode() int | ||
} | ||
|
||
sc := http.StatusInternalServerError | ||
if errors.As(err, &sce) { | ||
sc = sce.StatusCode() | ||
} | ||
|
||
render(data, sc, "application/json", rw) | ||
} | ||
|
||
func render(data []byte, statusCode int, contentType string, rw http.ResponseWriter) { | ||
rw.Header().Set("Content-Type", contentType) | ||
rw.WriteHeader(statusCode) | ||
rw.Write(data) | ||
} |
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,44 @@ | ||
package errors | ||
|
||
import ( | ||
"encoding/json" | ||
"errors" | ||
"net/http" | ||
) | ||
|
||
type internal struct { | ||
err error | ||
} | ||
|
||
func Internal(err error) error { | ||
return internal{err} | ||
} | ||
|
||
func (i internal) Message() string { | ||
return i.err.Error() | ||
} | ||
|
||
func (i internal) StatusCode() int { | ||
return http.StatusInternalServerError | ||
} | ||
|
||
func (i internal) String() string { | ||
return i.Message() | ||
} | ||
|
||
func (i internal) Error() string { | ||
return i.String() | ||
} | ||
|
||
func (i internal) MarshalJSON() ([]byte, error) { | ||
return json.Marshal(struct { | ||
Message string `json:"message"` | ||
}{ | ||
Message: i.Message(), | ||
}) | ||
} | ||
|
||
func IsInternal(err error) bool { | ||
var i internal | ||
return errors.As(err, &i) | ||
} |
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,50 @@ | ||
package errors | ||
|
||
import ( | ||
"encoding/json" | ||
"errors" | ||
"net/http" | ||
) | ||
|
||
type invalidArgument struct { | ||
reason, code string | ||
} | ||
|
||
func InvalidArgument(reason, code string) error { | ||
return invalidArgument{reason, code} | ||
} | ||
|
||
func (ia invalidArgument) Message() string { | ||
return ia.reason | ||
} | ||
|
||
func (ia invalidArgument) Code() string { | ||
return ia.code | ||
} | ||
|
||
func (ia invalidArgument) StatusCode() int { | ||
return http.StatusBadRequest | ||
} | ||
|
||
func (ia invalidArgument) String() string { | ||
return ia.Message() | ||
} | ||
|
||
func (ia invalidArgument) Error() string { | ||
return ia.String() | ||
} | ||
|
||
func (ia invalidArgument) MarshalJSON() ([]byte, error) { | ||
return json.Marshal(struct { | ||
Message string `json:"message"` | ||
Code string `json:"code"` | ||
}{ | ||
Message: ia.Message(), | ||
Code: ia.Code(), | ||
}) | ||
} | ||
|
||
func IsInvalidArgument(err error) bool { | ||
var ia invalidArgument | ||
return errors.As(err, &ia) | ||
} |
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,43 @@ | ||
package errors | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"net/http" | ||
) | ||
|
||
type notFound struct { | ||
resource, code string | ||
} | ||
|
||
func NotFound(resource, code string) error { | ||
return notFound{ | ||
resource: resource, | ||
code: code, | ||
} | ||
} | ||
|
||
func (nf notFound) Message() string { | ||
return fmt.Sprintf("%s not found", nf.resource) | ||
} | ||
|
||
func (nf notFound) Code() string { | ||
return nf.code | ||
} | ||
|
||
func (nf notFound) StatusCode() int { | ||
return http.StatusNotFound | ||
} | ||
|
||
func (nf notFound) String() string { | ||
return nf.Message() | ||
} | ||
|
||
func (nf notFound) Error() string { | ||
return nf.String() | ||
} | ||
|
||
func IsNotFound(err error) bool { | ||
var nf notFound | ||
return errors.As(err, &nf) | ||
} |
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,50 @@ | ||
package errors | ||
|
||
import ( | ||
"encoding/json" | ||
"errors" | ||
"net/http" | ||
) | ||
|
||
type unauthorized struct { | ||
reason, code string | ||
} | ||
|
||
func Unauthorized(reason, code string) error { | ||
return unauthorized{reason, code} | ||
} | ||
|
||
func (u unauthorized) Message() string { | ||
return u.reason | ||
} | ||
|
||
func (u unauthorized) Code() string { | ||
return u.code | ||
} | ||
|
||
func (u unauthorized) StatusCode() int { | ||
return http.StatusUnauthorized | ||
} | ||
|
||
func (u unauthorized) String() string { | ||
return u.Message() | ||
} | ||
|
||
func (u unauthorized) Error() string { | ||
return u.String() | ||
} | ||
|
||
func (u unauthorized) MarshalJSON() ([]byte, error) { | ||
return json.Marshal(struct { | ||
Message string `json:"message"` | ||
Code string `json:"code"` | ||
}{ | ||
Message: u.Message(), | ||
Code: u.Code(), | ||
}) | ||
} | ||
|
||
func IsUnauthorized(err error) bool { | ||
var u unauthorized | ||
return errors.As(err, &u) | ||
} |
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,9 @@ | ||
package redis | ||
|
||
const ( | ||
ErrRedisNilMessage = "redis: nil" | ||
) | ||
|
||
func IsErrRedisNilMessage(err error) bool { | ||
return err.Error() == ErrRedisNilMessage | ||
} |
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
Oops, something went wrong.