/
data_source_okta_auth_server_claims.go
107 lines (102 loc) · 3.3 KB
/
data_source_okta_auth_server_claims.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package okta
import (
"context"
"fmt"
"hash/crc32"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/okta/terraform-provider-okta/sdk"
)
func dataSourceAuthServerClaims() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceAuthServerClaimsRead,
Schema: map[string]*schema.Schema{
"auth_server_id": {
Type: schema.TypeString,
Required: true,
Description: "Auth server ID",
},
"claims": {
Type: schema.TypeList,
Computed: true,
Description: "Collection of authorization server claims retrieved from Okta with the following properties.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Computed: true,
Description: "ID of the claim.",
},
"name": {
Type: schema.TypeString,
Computed: true,
Description: "Name of the claim.",
},
"scopes": {
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
Description: "Specifies the scopes for this Claim.",
},
"status": {
Type: schema.TypeString,
Computed: true,
Description: "Status of the claim.",
},
"value": {
Type: schema.TypeString,
Computed: true,
Description: "Value of the claim.",
},
"value_type": {
Type: schema.TypeString,
Computed: true,
Description: "Specifies whether the Claim is an Okta EL expression (`EXPRESSION`), a set of groups (`GROUPS`), or a system claim (`SYSTEM`)",
},
"claim_type": {
Type: schema.TypeString,
Computed: true,
Description: "Specifies whether the Claim is for an access token (`RESOURCE`) or ID token (`IDENTITY`).",
},
"always_include_in_token": {
Type: schema.TypeBool,
Computed: true,
Description: "Specifies whether to include Claims in the token.",
},
},
},
},
},
Description: "Get a list of authorization server claims from Okta.",
}
}
func dataSourceAuthServerClaimsRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
claims, _, err := getOktaClientFromMetadata(m).AuthorizationServer.ListOAuth2Claims(ctx, d.Get("auth_server_id").(string))
if err != nil {
return diag.Errorf("failed to list authorization server claims: %v", err)
}
var s string
arr := make([]map[string]interface{}, len(claims))
for i := range claims {
s += claims[i].Name
arr[i] = flattenClaim(claims[i])
}
_ = d.Set("claims", arr)
d.SetId(fmt.Sprintf("%s.%d", d.Get("auth_server_id").(string), crc32.ChecksumIEEE([]byte(s))))
return nil
}
func flattenClaim(c *sdk.OAuth2Claim) map[string]interface{} {
m := map[string]interface{}{
"id": c.Id,
"name": c.Name,
"status": c.Status,
"value": c.Value,
"value_type": c.ValueType,
"claim_type": c.ClaimType,
"always_include_in_token": c.AlwaysIncludeInToken,
}
if c.Conditions != nil && len(c.Conditions.Scopes) > 0 {
m["scopes"] = convertStringSliceToSet(c.Conditions.Scopes)
}
return m
}