Skip to content

Commit 167ce6e

Browse files
tobiasbptechknowlogick
authored andcommitted
add-scope-to-token (#33)
This PR adds the ability to set scopes for tokens (they can not be used for much without). Removed the _username_ from the _token resource_ as the owner can not be configured, as it will be owned by the user creating the resource. As far as I can tell, it's not possible to modify the scopes for a existing token using the API, so a token created by the provider will be recreated if the list of scopes is updated. This reflects what is possible using the GUI. This PR fixes this issue: https://gitea.com/gitea/terraform-provider-gitea/issues/32 Reviewed-on: https://gitea.com/gitea/terraform-provider-gitea/pulls/33 Co-authored-by: tobiasbp <[email protected]> Co-committed-by: tobiasbp <[email protected]>
1 parent 557ea26 commit 167ce6e

File tree

3 files changed

+58
-33
lines changed

3 files changed

+58
-33
lines changed

docs/resources/token.md

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -30,18 +30,10 @@ provider "gitea" {
3030
password = var.gitea_password
3131
}
3232
33-
resource "gitea_user" "test" {
34-
username = "test"
35-
login_name = "test"
36-
password = "Geheim1!"
37-
38-
must_change_password = false
39-
admin = true
40-
}
41-
33+
// The token owner is the creator of the token
4234
resource "gitea_token" "test_token" {
43-
username = resource.gitea_user.test.username
44-
name = "test-token"
35+
name = "test_token"
36+
scopes = ["all"]
4537
}
4638
4739
output "token" {
@@ -56,7 +48,7 @@ output "token" {
5648
### Required
5749

5850
- `name` (String) The name of the Access Token
59-
- `username` (String) The owner of the Access Token
51+
- `scopes` (Set of String) List of string representations of scopes for the token
6052

6153
### Read-Only
6254

examples/resources/gitea_token/resource.tf

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,10 @@ provider "gitea" {
55
password = var.gitea_password
66
}
77

8-
resource "gitea_user" "test" {
9-
username = "test"
10-
login_name = "test"
11-
password = "Geheim1!"
12-
13-
must_change_password = false
14-
admin = true
15-
}
16-
8+
// The token owner is the creator of the token
179
resource "gitea_token" "test_token" {
18-
username = resource.gitea_user.test.username
19-
name = "test-token"
10+
name = "test_token"
11+
scopes = ["all"]
2012
}
2113

2214
output "token" {

gitea/resource_gitea_token.go

Lines changed: 51 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,36 @@ import (
99
)
1010

1111
const (
12-
TokenUsername string = "username"
1312
TokenName string = "name"
1413
TokenHash string = "token"
1514
TokenLastEight string = "last_eight"
15+
TokenScopes string = "scopes"
1616
)
1717

18+
// validScopes contains the valid scopes for tokens as listed
19+
// at https://docs.gitea.com/development/oauth2-provider#scopes
20+
var validScopes = map[string]bool{
21+
"all": true,
22+
"read:activitypub": true,
23+
"write:activitypub": true,
24+
"read:admin": true,
25+
"write:admin": true,
26+
"read:issue": true,
27+
"write:issue": true,
28+
"read:misc": true,
29+
"write:misc": true,
30+
"read:notification": true,
31+
"write:notification": true,
32+
"read:organization": true,
33+
"write:organization": true,
34+
"read:package": true,
35+
"write:package": true,
36+
"read:repository": true,
37+
"write:repository": true,
38+
"read:user": true,
39+
"write:user": true,
40+
}
41+
1842
func searchTokenById(c *gitea.Client, id int64) (res *gitea.AccessToken, err error) {
1943
page := 1
2044

@@ -47,10 +71,23 @@ func resourceTokenCreate(d *schema.ResourceData, meta interface{}) (err error) {
4771

4872
client := meta.(*gitea.Client)
4973

50-
var opt gitea.CreateAccessTokenOption
51-
opt.Name = d.Get(TokenName).(string)
74+
// Create a list of valid scopes. Thrown an error if an invalid scope is found
75+
var scopes []gitea.AccessTokenScope
76+
for _, s := range d.Get(TokenScopes).(*schema.Set).List() {
77+
s := s.(string)
78+
if validScopes[s] {
79+
scopes = append(scopes, gitea.AccessTokenScope(s))
80+
} else {
81+
return fmt.Errorf("Invalid token scope: '%s'", s)
82+
}
83+
}
84+
85+
opts := gitea.CreateAccessTokenOption{
86+
Name: d.Get(TokenName).(string),
87+
Scopes: scopes,
88+
}
5289

53-
token, _, err := client.CreateAccessToken(opt)
90+
token, _, err := client.CreateAccessToken(opts)
5491

5592
if err != nil {
5693
return err
@@ -106,6 +143,7 @@ func setTokenResourceData(token *gitea.AccessToken, d *schema.ResourceData) (err
106143
d.Set(TokenHash, token.Token)
107144
}
108145
d.Set(TokenLastEight, token.TokenLastEight)
146+
d.Set(TokenScopes, token.Scopes)
109147

110148
return
111149
}
@@ -119,12 +157,6 @@ func resourceGiteaToken() *schema.Resource {
119157
StateContext: schema.ImportStatePassthroughContext,
120158
},
121159
Schema: map[string]*schema.Schema{
122-
"username": {
123-
Type: schema.TypeString,
124-
Required: true,
125-
ForceNew: true,
126-
Description: "The owner of the Access Token",
127-
},
128160
"name": {
129161
Type: schema.TypeString,
130162
Required: true,
@@ -141,6 +173,15 @@ func resourceGiteaToken() *schema.Resource {
141173
Type: schema.TypeString,
142174
Computed: true,
143175
},
176+
"scopes": {
177+
Type: schema.TypeSet,
178+
Elem: &schema.Schema{
179+
Type: schema.TypeString,
180+
},
181+
Required: true,
182+
ForceNew: true,
183+
Description: "List of string representations of scopes for the token",
184+
},
144185
},
145186
Description: "`gitea_token` manages gitea Access Tokens.\n\n" +
146187
"Due to upstream limitations (see https://gitea.com/gitea/go-sdk/issues/610) this resource\n" +

0 commit comments

Comments
 (0)