diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..996352e --- /dev/null +++ b/go.mod @@ -0,0 +1,9 @@ +module github.com/chiuwah/fd_oauth-go + +go 1.14 + +require ( + github.com/chiuwah/fd_utils-go v0.1.0 + github.com/federicoleon/golang-restclient v0.0.0-20191104170228-162ed620df66 + github.com/stretchr/testify v1.5.1 // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..bde75ed --- /dev/null +++ b/go.sum @@ -0,0 +1,15 @@ +github.com/chiuwah/fd_utils-go v0.1.0 h1:6yN6HIOESTiiNWY5ZGd4ayS96OdHTxPVXnuM8VLWhPM= +github.com/chiuwah/fd_utils-go v0.1.0/go.mod h1:6Tbw5hgblKbNyMTeD0fhpmhGbwpm3pQLJ2oRxxa2DuM= +github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/federicoleon/golang-restclient v0.0.0-20191104170228-162ed620df66 h1:QESWlOIiziWcM4bCvtT7TFg4PXjudKMnO3ArjxuIJj4= +github.com/federicoleon/golang-restclient v0.0.0-20191104170228-162ed620df66/go.mod h1:lvjF6h5LrES4h39ha/njlBBBqCmISh6BNSEiXH+mT5k= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.5.1 h1:nOGnQDM7FYENwehXlg/kFVnos3rEvtKTjRvOWSzb6H4= +github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw= +gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= diff --git a/oauth/errors/errors.go b/oauth/errors/errors.go deleted file mode 100644 index 6289841..0000000 --- a/oauth/errors/errors.go +++ /dev/null @@ -1,35 +0,0 @@ -package errors - -import ( - "net/http" -) - -type RestErr struct { - Message string `json:"message"` - Status int `json:"status"` - Error string `json:"error"` -} - -func NewBadRequestError(message string) *RestErr { - return &RestErr{ - Message: message, - Status: http.StatusBadRequest, - Error: "bad_request", - } -} - -func NewNotFoundError(message string) *RestErr { - return &RestErr{ - Message: message, - Status: http.StatusNotFound, - Error: "not_found", - } -} - -func NewInternalServerError(message string) *RestErr { - return &RestErr{ - Message: message, - Status: http.StatusInternalServerError, - Error: "internal_server_error", - } -} diff --git a/oauth/oauth.go b/oauth/oauth.go index f2bc7d2..dafe42b 100644 --- a/oauth/oauth.go +++ b/oauth/oauth.go @@ -3,7 +3,7 @@ package oauth import ( "encoding/json" "fmt" - "github.com/chiuwah/fd_oauth-go/oauth/errors" + "github.com/chiuwah/fd_utils-go/rest_errors" "github.com/federicoleon/golang-restclient/rest" "net/http" "strconv" @@ -61,14 +61,14 @@ func GetClientId(request *http.Request) int64 { return clientId } -func AuthenticateRequest(request *http.Request) *errors.RestErr { +func AuthenticateRequest(request *http.Request) *rest_errors.RestErr { if request == nil { return nil } cleanRequest(request) - accessTokenId := strings.TrimSpace(request.URL.Query().Get(paramAccessToken)) + accessTokenId := strings.TrimSpace(request.URL. Query().Get(paramAccessToken)) if accessTokenId == "" { return nil } @@ -96,22 +96,22 @@ func cleanRequest(request *http.Request) { } -func getAccessToken(accessTokenId string) (*accessToken, *errors.RestErr) { +func getAccessToken(accessTokenId string) (*accessToken, *rest_errors.RestErr) { response := oauthRestClient.Get(fmt.Sprintf("/oauth/access_token/%s", accessTokenId)) if response == nil || response.Response == nil { - return nil, errors.NewInternalServerError("invalid restclient response when trying to get access token") + return nil, rest_errors.NewInternalServerError("invalid restclient response when trying to get access token", rest_errors.NewError("invalid response")) } if response.StatusCode > 299 { - var restErr errors.RestErr + var restErr rest_errors.RestErr if err := json.Unmarshal(response.Bytes(), &restErr); err != nil { - return nil, errors.NewInternalServerError("invalid error interface when trying to get access token") + return nil, rest_errors.NewInternalServerError("invalid error interface when trying to get access token", err) } return nil, &restErr } var at accessToken if err := json.Unmarshal(response.Bytes(), &at); err != nil { - return nil, errors.NewInternalServerError("error when trying to unmarshal access token response") + return nil, rest_errors.NewInternalServerError("error when trying to unmarshal access token response", err) } return &at, nil }