summaryrefslogtreecommitdiffstats
path: root/model/authorize.go
diff options
context:
space:
mode:
authorJoram Wilander <jwawilander@gmail.com>2017-04-20 09:55:02 -0400
committerGitHub <noreply@github.com>2017-04-20 09:55:02 -0400
commitbe9624e2adce7c95039e62fc4ee22538d7fa2d2f (patch)
tree318179b4d3a4cb5114f887797a5a4c836e5255d7 /model/authorize.go
parent1a0f8d1b3c7451eac43bfdc5971de060caabf441 (diff)
downloadchat-be9624e2adce7c95039e62fc4ee22538d7fa2d2f.tar.gz
chat-be9624e2adce7c95039e62fc4ee22538d7fa2d2f.tar.bz2
chat-be9624e2adce7c95039e62fc4ee22538d7fa2d2f.zip
Implement v4 endpoints for OAuth (#6040)
* Implement POST /oauth/apps endpoint for APIv4 * Implement GET /oauth/apps endpoint for APIv4 * Implement GET /oauth/apps/{app_id} and /oauth/apps/{app_id}/info endpoints for APIv4 * Refactor API version independent oauth endpoints * Implement DELETE /oauth/apps/{app_id} endpoint for APIv4 * Implement /oauth/apps/{app_id}/regen_secret endpoint for APIv4 * Implement GET /user/{user_id}/oauth/apps/authorized endpoint for APIv4 * Implement POST /oauth/deauthorize endpoint
Diffstat (limited to 'model/authorize.go')
-rw-r--r--model/authorize.go56
1 files changed, 56 insertions, 0 deletions
diff --git a/model/authorize.go b/model/authorize.go
index 2f290fab2..460b70823 100644
--- a/model/authorize.go
+++ b/model/authorize.go
@@ -6,6 +6,7 @@ package model
import (
"encoding/json"
"io"
+ "net/http"
)
const (
@@ -25,6 +26,14 @@ type AuthData struct {
Scope string `json:"scope"`
}
+type AuthorizeRequest struct {
+ ResponseType string `json:"response_type"`
+ ClientId string `json:"client_id"`
+ RedirectUri string `json:"redirect_uri"`
+ Scope string `json:"scope"`
+ State string `json:"state"`
+}
+
// IsValid validates the AuthData and returns an error if it isn't configured
// correctly.
func (ad *AuthData) IsValid() *AppError {
@@ -64,6 +73,33 @@ func (ad *AuthData) IsValid() *AppError {
return nil
}
+// IsValid validates the AuthorizeRequest and returns an error if it isn't configured
+// correctly.
+func (ar *AuthorizeRequest) IsValid() *AppError {
+
+ if len(ar.ClientId) != 26 {
+ return NewAppError("AuthData.IsValid", "model.authorize.is_valid.client_id.app_error", nil, "", http.StatusBadRequest)
+ }
+
+ if len(ar.ResponseType) == 0 {
+ return NewAppError("AuthData.IsValid", "model.authorize.is_valid.response_type.app_error", nil, "", http.StatusBadRequest)
+ }
+
+ if len(ar.RedirectUri) == 0 || len(ar.RedirectUri) > 256 || !IsValidHttpUrl(ar.RedirectUri) {
+ return NewAppError("AuthData.IsValid", "model.authorize.is_valid.redirect_uri.app_error", nil, "client_id="+ar.ClientId, http.StatusBadRequest)
+ }
+
+ if len(ar.State) > 128 {
+ return NewAppError("AuthData.IsValid", "model.authorize.is_valid.state.app_error", nil, "client_id="+ar.ClientId, http.StatusBadRequest)
+ }
+
+ if len(ar.Scope) > 128 {
+ return NewAppError("AuthData.IsValid", "model.authorize.is_valid.scope.app_error", nil, "client_id="+ar.ClientId, http.StatusBadRequest)
+ }
+
+ return nil
+}
+
func (ad *AuthData) PreSave() {
if ad.ExpiresIn == 0 {
ad.ExpiresIn = AUTHCODE_EXPIRE_TIME
@@ -98,6 +134,26 @@ func AuthDataFromJson(data io.Reader) *AuthData {
}
}
+func (ar *AuthorizeRequest) ToJson() string {
+ b, err := json.Marshal(ar)
+ if err != nil {
+ return ""
+ } else {
+ return string(b)
+ }
+}
+
+func AuthorizeRequestFromJson(data io.Reader) *AuthorizeRequest {
+ decoder := json.NewDecoder(data)
+ var ar AuthorizeRequest
+ err := decoder.Decode(&ar)
+ if err == nil {
+ return &ar
+ } else {
+ return nil
+ }
+}
+
func (ad *AuthData) IsExpired() bool {
if GetMillis() > ad.CreateAt+int64(ad.ExpiresIn*1000) {