mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2026-05-20 01:36:37 +00:00
Extends the UI introduced in #12558 to have edit capabilities. (not in scope: "Add" for a new Authorized Integration will be the next update to this UI; `create-authorized-integration` CLI is still the only way to create a new record) This PR includes a few refactoring steps. The goal of these steps is to have `services/auth` be a single entrypoint for validating, inserting, or updating an authorized integration. Some logic is moved out of `services/authz` because it is not authorization related, and some is moved out of `services/auth/method` to allow it to be reused during validation without creating a cyclical module dependency. This PR also adds comprehensive validation to the more complex fields in the authorized integration, such as the issuer and claim rules. This validation applies to the `forgejo admin user create-authorized-integration` CLI as well. The visible UI is the same as #12558, but with a "Save" button, and the ability to display errors:  ## Checklist The [contributor guide](https://forgejo.org/docs/next/contributor/) contains information that will be helpful to first time contributors. All work and communication must conform to Forgejo's [AI Agreement](https://codeberg.org/forgejo/governance/src/branch/main/AIAgreement.md). There also are a few [conditions for merging Pull Requests in Forgejo repositories](https://codeberg.org/forgejo/governance/src/branch/main/PullRequestsAgreement.md). You are also welcome to join the [Forgejo development chatroom](https://matrix.to/#/#forgejo-development:matrix.org). ### Tests for Go changes - I added test coverage for Go changes... - [x] in their respective `*_test.go` for unit tests. - [ ] in the `tests/integration` directory if it involves interactions with a live Forgejo server. - I ran... - [x] `make pr-go` before pushing ### Tests for JavaScript changes - I added test coverage for JavaScript changes... - [ ] in `web_src/js/*.test.js` if it can be unit tested. - [x] in `tests/e2e/*.test.e2e.js` if it requires interactions with a live Forgejo server (see also the [developer guide for JavaScript testing](https://codeberg.org/forgejo/forgejo/src/branch/forgejo/tests/e2e/README.md#end-to-end-tests)). ### Documentation - [ ] I created a pull request [to the documentation](https://codeberg.org/forgejo/docs) to explain to Forgejo users how to use this change. - [x] I did not document these changes and I do not expect someone else to do it. - Documentation is on my TODO list and will be completed before release. ### Release notes - [x] This change will be noticed by a Forgejo user or admin (feature, bug fix, performance, etc.). I suggest to include a release note for this change. - [ ] This change is not visible to a Forgejo user or admin (refactor, dependency upgrade, etc.). I think there is no need to add a release note for this change. Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/12601 Reviewed-by: Andreas Ahlenstorf <aahlenst@noreply.codeberg.org>
173 lines
4.3 KiB
Go
173 lines
4.3 KiB
Go
// Copyright 2020 The Gitea Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package json
|
|
|
|
// Allow "encoding/json" import.
|
|
import (
|
|
"bytes"
|
|
"encoding/binary"
|
|
"encoding/json" //nolint:depguard
|
|
"io"
|
|
|
|
jsoniter "github.com/json-iterator/go"
|
|
)
|
|
|
|
// Encoder represents an encoder for json
|
|
type Encoder interface {
|
|
Encode(v any) error
|
|
}
|
|
|
|
// Decoder represents a decoder for json
|
|
type Decoder interface {
|
|
Decode(v any) error
|
|
DisallowUnknownFields()
|
|
}
|
|
|
|
// Interface represents an interface to handle json data
|
|
type Interface interface {
|
|
Marshal(v any) ([]byte, error)
|
|
Unmarshal(data []byte, v any) error
|
|
NewEncoder(writer io.Writer) Encoder
|
|
NewDecoder(reader io.Reader) Decoder
|
|
Indent(dst *bytes.Buffer, src []byte, prefix, indent string) error
|
|
}
|
|
|
|
var (
|
|
// DefaultJSONHandler default json handler
|
|
DefaultJSONHandler Interface = JSONiter{jsoniter.ConfigCompatibleWithStandardLibrary}
|
|
|
|
_ Interface = StdJSON{}
|
|
_ Interface = JSONiter{}
|
|
)
|
|
|
|
// StdJSON implements Interface via encoding/json
|
|
type StdJSON struct{}
|
|
|
|
// Marshal implements Interface
|
|
func (StdJSON) Marshal(v any) ([]byte, error) {
|
|
return json.Marshal(v)
|
|
}
|
|
|
|
// Unmarshal implements Interface
|
|
func (StdJSON) Unmarshal(data []byte, v any) error {
|
|
return json.Unmarshal(data, v)
|
|
}
|
|
|
|
// NewEncoder implements Interface
|
|
func (StdJSON) NewEncoder(writer io.Writer) Encoder {
|
|
return json.NewEncoder(writer)
|
|
}
|
|
|
|
// NewDecoder implements Interface
|
|
func (StdJSON) NewDecoder(reader io.Reader) Decoder {
|
|
return json.NewDecoder(reader)
|
|
}
|
|
|
|
// Indent implements Interface
|
|
func (StdJSON) Indent(dst *bytes.Buffer, src []byte, prefix, indent string) error {
|
|
return json.Indent(dst, src, prefix, indent)
|
|
}
|
|
|
|
// JSONiter implements Interface via jsoniter
|
|
type JSONiter struct {
|
|
jsoniter.API
|
|
}
|
|
|
|
// Marshal implements Interface
|
|
func (j JSONiter) Marshal(v any) ([]byte, error) {
|
|
return j.API.Marshal(v)
|
|
}
|
|
|
|
// Unmarshal implements Interface
|
|
func (j JSONiter) Unmarshal(data []byte, v any) error {
|
|
return j.API.Unmarshal(data, v)
|
|
}
|
|
|
|
// NewEncoder implements Interface
|
|
func (j JSONiter) NewEncoder(writer io.Writer) Encoder {
|
|
return j.API.NewEncoder(writer)
|
|
}
|
|
|
|
// NewDecoder implements Interface
|
|
func (j JSONiter) NewDecoder(reader io.Reader) Decoder {
|
|
return j.API.NewDecoder(reader)
|
|
}
|
|
|
|
// Indent implements Interface, since jsoniter don't support Indent, just use encoding/json's
|
|
func (j JSONiter) Indent(dst *bytes.Buffer, src []byte, prefix, indent string) error {
|
|
return json.Indent(dst, src, prefix, indent)
|
|
}
|
|
|
|
// Marshal converts object as bytes
|
|
func Marshal(v any) ([]byte, error) {
|
|
return DefaultJSONHandler.Marshal(v)
|
|
}
|
|
|
|
// Unmarshal decodes object from bytes
|
|
func Unmarshal(data []byte, v any) error {
|
|
return DefaultJSONHandler.Unmarshal(data, v)
|
|
}
|
|
|
|
// NewEncoder creates an encoder to write objects to writer
|
|
func NewEncoder(writer io.Writer) Encoder {
|
|
return DefaultJSONHandler.NewEncoder(writer)
|
|
}
|
|
|
|
// NewDecoder creates a decoder to read objects from reader
|
|
func NewDecoder(reader io.Reader) Decoder {
|
|
return DefaultJSONHandler.NewDecoder(reader)
|
|
}
|
|
|
|
// Indent appends to dst an indented form of the JSON-encoded src.
|
|
func Indent(dst *bytes.Buffer, src []byte, prefix, indent string) error {
|
|
return DefaultJSONHandler.Indent(dst, src, prefix, indent)
|
|
}
|
|
|
|
// MarshalIndent copied from encoding/json
|
|
func MarshalIndent(v any, prefix, indent string) ([]byte, error) {
|
|
b, err := Marshal(v)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
var buf bytes.Buffer
|
|
err = Indent(&buf, b, prefix, indent)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return buf.Bytes(), nil
|
|
}
|
|
|
|
// Valid proxy to json.Valid
|
|
func Valid(data []byte) bool {
|
|
return json.Valid(data)
|
|
}
|
|
|
|
// UnmarshalHandleDoubleEncode - due to a bug in xorm (see https://gitea.com/xorm/xorm/pulls/1957) - it's
|
|
// possible that a Blob may be double encoded or gain an unwanted prefix of 0xff 0xfe.
|
|
func UnmarshalHandleDoubleEncode(bs []byte, v any) error {
|
|
err := json.Unmarshal(bs, v)
|
|
if err != nil {
|
|
ok := true
|
|
rs := []byte{}
|
|
temp := make([]byte, 2)
|
|
for _, rn := range string(bs) {
|
|
if rn > 0xffff {
|
|
ok = false
|
|
break
|
|
}
|
|
binary.LittleEndian.PutUint16(temp, uint16(rn))
|
|
rs = append(rs, temp...)
|
|
}
|
|
if ok {
|
|
if len(rs) > 1 && rs[0] == 0xff && rs[1] == 0xfe {
|
|
rs = rs[2:]
|
|
}
|
|
err = json.Unmarshal(rs, v)
|
|
}
|
|
}
|
|
if err != nil && len(bs) > 2 && bs[0] == 0xff && bs[1] == 0xfe {
|
|
err = json.Unmarshal(bs[2:], v)
|
|
}
|
|
return err
|
|
}
|