예제 #1
0
파일: usersapiv1.go 프로젝트: rechen/wharf
//There is nothing in request body, just authorization through Basic Authorization.
func (this *UserAPIV1Controller) GetUsers() {
	if username, passwd, err := utils.DecodeBasicAuth(this.Ctx.Input.Header("Authorization")); err != nil {
		this.JSONOut(http.StatusUnauthorized, err.Error(), nil)
		return
	} else {
		user := new(models.User)

		if err := user.Get(username, passwd); err != nil {
			this.JSONOut(http.StatusUnauthorized, err.Error(), nil)
			return
		}

		memo, _ := json.Marshal(this.Ctx.Input.Header)
		user.Log(models.ACTION_SIGNUP, models.LEVELINFORMATIONAL, models.TYPE_APIV1, user.Id, memo)

		this.JSONOut(http.StatusOK, "User authorization successfully.", nil)
		return
	}
}
예제 #2
0
파일: userswebv1.go 프로젝트: rechen/wharf
func (this *UserWebAPIV1Controller) Signin() {
	user := new(models.User)

	if err := json.Unmarshal(this.Ctx.Input.CopyBody(), &user); err != nil {
		this.JSONOut(http.StatusBadRequest, err.Error(), nil)
		return
	} else {
		if err := user.Get(user.Username, user.Password); err != nil {
			this.JSONOut(http.StatusBadRequest, err.Error(), nil)
			return
		}

		memo, _ := json.Marshal(this.Ctx.Input.Header)
		user.Log(models.ACTION_SIGNIN, models.LEVELINFORMATIONAL, models.TYPE_WEBV1, user.Id, memo)

		this.Ctx.Input.CruSession.Set("user", user)

		this.JSONOut(http.StatusOK, "User singin successfully!", nil)
		return
	}
}
예제 #3
0
파일: pingapiv2.go 프로젝트: rechen/wharf
func (this *PingAPIV2Controller) GetPing() {
	if len(this.Ctx.Input.Header("Authorization")) == 0 {
		this.JSONOut(http.StatusUnauthorized, "", map[string][]modules.ErrorDescriptor{"errors": []modules.ErrorDescriptor{modules.ErrorDescriptors[modules.APIErrorCodeUnauthorized]}})
		return
	}

	if username, passwd, err := utils.DecodeBasicAuth(this.Ctx.Input.Header("Authorization")); err != nil {
		this.JSONOut(http.StatusUnauthorized, "", map[string][]modules.ErrorDescriptor{"errors": []modules.ErrorDescriptor{modules.ErrorDescriptors[modules.APIErrorCodeUnauthorized]}})
		return
	} else {
		user := new(models.User)

		if err := user.Get(username, passwd); err != nil {
			this.JSONOut(http.StatusUnauthorized, "", map[string][]modules.ErrorDescriptor{"errors": []modules.ErrorDescriptor{modules.ErrorDescriptors[modules.APIErrorCodeUnauthorized]}})
			return
		}

		memo, _ := json.Marshal(this.Ctx.Input.Header)
		user.Log(models.ACTION_SIGNUP, models.LEVELINFORMATIONAL, models.TYPE_APIV2, user.Id, memo)

		this.JSONOut(http.StatusOK, "", "User authorization successfully.")
		return
	}
}
예제 #4
0
파일: auth.go 프로젝트: rechen/wharf
func FilterAuth(ctx *context.Context) {
	var namespace, repository string
	var permission int

	auth := true
	user := new(models.User)

	namespace = strings.Split(string(ctx.Input.Params[":splat"]), "/")[0]
	repository = strings.Split(string(ctx.Input.Params[":splat"]), "/")[1]

	//Get Permission
	permission = getPermission(ctx.Input.Method())

	//Check Authorization In Header
	if len(ctx.Input.Header("Authorization")) == 0 || strings.Index(ctx.Input.Header("Authorization"), "Basic") == -1 {
		auth = false
		goto AUTH
	}

	//Check Username, Password And Get User
	if username, passwd, err := utils.DecodeBasicAuth(ctx.Input.Header("Authorization")); err != nil {
		auth = false
		goto AUTH
	} else {
		if err := user.Get(username, passwd); err != nil {
			auth = false
			goto AUTH
		}
	}

	//Docker Registry V1 Image Don't Check User/Org Permission
	if isImageResource(ctx.Request.URL.String()) == true {
		goto AUTH
	}

	//Username != namespace
	if user.Username != namespace {
		u := new(models.User)
		if has, _, err := u.Has(namespace); err != nil {
			auth = false
			goto AUTH
		} else if has == false {
			//Org Repository Check
			auth = checkOrgRepositoryPermission(user, namespace, repository, permission)
		} else if has == true {
			//Different User and Public/Private Repository
			auth = checkRepositoriesPrivate(namespace, repository)
		}
	}

AUTH:
	if auth == false {
		result := map[string][]modules.ErrorDescriptor{"errors": []modules.ErrorDescriptor{modules.ErrorDescriptors[modules.APIErrorCodeUnauthorized]}}

		data, _ := json.Marshal(result)

		ctx.Output.Context.Output.SetStatus(http.StatusNotFound)
		ctx.Output.Context.Output.Body(data)
		return
	}
}