Skip to content

Commit 8265bde

Browse files
authored
Merge branch 'main' into lunny/update_default_branch_btn
2 parents 92d8a30 + c3aaf5e commit 8265bde

File tree

2 files changed

+41
-22
lines changed

2 files changed

+41
-22
lines changed

docs/content/doc/developers/api-usage.en-us.md

+36-22
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,42 @@ better understand this by looking at the code -- as of this writing,
4040
Gitea parses queries and headers to find the token in
4141
[modules/auth/auth.go](https://github.com/go-gitea/gitea/blob/6efdcaed86565c91a3dc77631372a9cc45a58e89/modules/auth/auth.go#L47).
4242

43-
You can create an API key token via your Gitea installation's web interface:
44-
`Settings | Applications | Generate New Token`.
43+
## Generating and listing API tokens
44+
45+
A new token can be generated with a `POST` request to
46+
`/users/:name/tokens`.
47+
48+
Note that `/users/:name/tokens` is a special endpoint and requires you
49+
to authenticate using `BasicAuth` and a password, as follows:
50+
51+
52+
```sh
53+
$ curl -XPOST -H "Content-Type: application/json" -k -d '{"name":"test"}' -u username:password https://gitea.your.host/api/v1/users/<username>/tokens
54+
{"id":1,"name":"test","sha1":"9fcb1158165773dd010fca5f0cf7174316c3e37d","token_last_eight":"16c3e37d"}
55+
```
56+
57+
The ``sha1`` (the token) is only returned once and is not stored in
58+
plain-text. It will not be displayed when listing tokens with a `GET`
59+
request; e.g.
60+
61+
```sh
62+
$ curl --request GET --url https://yourusername:password@gitea.your.host/api/v1/users/<username>/tokens
63+
[{"name":"test","sha1":"","token_last_eight:"........":},{"name":"dev","sha1":"","token_last_eight":"........"}]
64+
```
65+
66+
To use the API with basic authentication with two factor authentication
67+
enabled, you'll need to send an additional header that contains the one
68+
time password (6 digitrotating token).
69+
An example of the header is `X-Gitea-OTP: 123456` where `123456`
70+
is where you'd place the code from your authenticator.
71+
Here is how the request would look like in curl:
72+
73+
```sh
74+
$ curl -H "X-Gitea-OTP: 123456" --request GET --url https://yourusername:yourpassword@gitea.your.host/api/v1/users/yourusername/tokens
75+
```
76+
77+
You can also create an API key token via your Gitea installation's web
78+
interface: `Settings | Applications | Generate New Token`.
4579
4680
## OAuth2 Provider
4781
@@ -82,26 +116,6 @@ or on
82116
The OpenAPI document is at:
83117
`https://gitea.your.host/swagger.v1.json`
84118
85-
## Listing your issued tokens via the API
86-
87-
As mentioned in
88-
[#3842](https://github.com/go-gitea/gitea/issues/3842#issuecomment-397743346),
89-
`/users/:name/tokens` is special and requires you to authenticate
90-
using BasicAuth, as follows:
91-
92-
### Using basic authentication:
93-
94-
```sh
95-
$ curl --request GET --url https://yourusername:yourpassword@gitea.your.host/api/v1/users/yourusername/tokens
96-
[{"name":"test","sha1":"..."},{"name":"dev","sha1":"..."}]
97-
```
98-
99-
As of v1.8.0 of Gitea, if using basic authentication with the API and your user has two factor authentication enabled, you'll need to send an additional header that contains the one time password (6 digit rotating token). An example of the header is `X-Gitea-OTP: 123456` where `123456` is where you'd place the code from your authenticator. Here is how the request would look like in curl:
100-
101-
```sh
102-
$ curl -H "X-Gitea-OTP: 123456" --request GET --url https://yourusername:yourpassword@gitea.your.host/api/v1/users/yourusername/tokens
103-
```
104-
105119
## Sudo
106120
107121
The API allows admin users to sudo API requests as another user. Simply add either a `sudo=` parameter or `Sudo:` request header with the username of the user to sudo.

models/admin.go

+5
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,11 @@ func DeleteNotice(id int64) error {
114114

115115
// DeleteNotices deletes all notices with ID from start to end (inclusive).
116116
func DeleteNotices(start, end int64) error {
117+
if start == 0 && end == 0 {
118+
_, err := x.Exec("DELETE FROM notice")
119+
return err
120+
}
121+
117122
sess := x.Where("id >= ?", start)
118123
if end > 0 {
119124
sess.And("id <= ?", end)

0 commit comments

Comments
 (0)