Commit 37a0cb9b41cf7615de6a9d8f546e6712fb24a1f6

Parents: d624c651d60c89fce8aa8081a425fc49cd7fa598

From: Moritz Poldrack <git@moritz.sh>
Date: Thu Jan 11 11:43:06 2024 +0700

add examples

		

Stats

examples_test.go +53/-0

Changeset

 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
diff --git a/examples_test.go b/examples_test.go
new file mode 100644
index 0000000000000000000000000000000000000000..3445b149d4bd8c0b9d42daa7262bb4013d70a969
--- /dev/null
+++ b/examples_test.go
@@ -0,0 +1,53 @@
+//go:build example
+
+package oidc_test
+
+// SPDX-FileCopyrightText: © Moritz Poldrack
+// SPDX-License-Identifier: MPL-2.0
+
+import (
+	"fmt"
+	"strings"
+
+	"mpldr.codes/oidc"
+)
+
+func ExampleConfiguration_ParseJWT() {
+	const token = "eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJodHRwczovL3Nzby5wcm92aS5kZS9hcHBsaWNhdGlvbi9vL215LWNvb2wtYXBwLyIsInN1YiI6InVzdWFsbHkgd2F5IHRvbyBsb25nIiwiZXhwIjoxNzA0OTY4MTQxLCJpYXQiOjE3MDQ5NjYzNDEsImF1dGhfdGltZSI6MTcwNDk2NjM0MSwibmFtZSI6Ik1vcml0eiBQb2xkcmFjayIsImdyb3VwcyI6WyJhZG1pbiIsInVzZXJzIiwidnBuIl19.YTz718lKH8fU_uyaxM9lIKSc_ciM1nsCDzqkVjv6pZa6xIbhJmZdK43fnGm5S1NRqqQ554onqk0UroL5v47debT-hQcPCwAxuFg21qXgrIYZupbN-xZLITiodQKgUElTNLtDlwvO4rnd7VD5I2JGO4vrMK8zcSWQ9TQ5gzs6k00HRJWsUcM6Pv_MKti1unyCvBhCos4Oa3B_lOMDUx_FBHxkzSkEOdtOOeXByvcrz5CAY2lqWByMXAXl4oR95JYRk3cJuVaCT9v5X6ZREB6gej9f869aCcLjmOGImt84kIZlGBeaNdl9-DKHDxQaOhI6H6TGqyDxB6Kiy7zKxhv7mQ"
+
+	cfg, _ := oidc.Configure("https://sso.provi.de/application/o/my-cool-app/", "")
+
+	// no credentials needed
+
+	tok, _ := cfg.ParseJWT(token)
+
+	// Output: Moritz Poldrack
+	fmt.Println(tok.Name())
+}
+
+func ExampleConfiguration_GetAuthorizationURL() {
+	cfg, _ := oidc.Configure("https://sso.provi.de/application/o/my-cool-app/", "")
+
+	// credentials required for callback
+
+	redirectTo := cfg.GetAuthorizationURL()
+
+	// Output: https://sso.provi.de/application/o/authorize/?access_type=online&client_id=&code_challenge=oyQwzDDJjdk7v3zbcyrI2W6a2tJP5cIWhHkI5m4d6wE&code_challenge_method=S256&response_type=code&scope=openid+email+profile&state=7a705c4650a57e5a
+	fmt.Println(redirectTo)
+}
+
+func ExampleToken_GetField() {
+	const token = "eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJodHRwczovL3Nzby5wcm92aS5kZS9hcHBsaWNhdGlvbi9vL215LWNvb2wtYXBwLyIsInN1YiI6InVzdWFsbHkgd2F5IHRvbyBsb25nIiwiZXhwIjoxNzA0OTY4MTQxLCJpYXQiOjE3MDQ5NjYzNDEsImF1dGhfdGltZSI6MTcwNDk2NjM0MSwibmFtZSI6Ik1vcml0eiBQb2xkcmFjayIsImdyb3VwcyI6WyJhZG1pbiIsInVzZXJzIiwidnBuIl19.YTz718lKH8fU_uyaxM9lIKSc_ciM1nsCDzqkVjv6pZa6xIbhJmZdK43fnGm5S1NRqqQ554onqk0UroL5v47debT-hQcPCwAxuFg21qXgrIYZupbN-xZLITiodQKgUElTNLtDlwvO4rnd7VD5I2JGO4vrMK8zcSWQ9TQ5gzs6k00HRJWsUcM6Pv_MKti1unyCvBhCos4Oa3B_lOMDUx_FBHxkzSkEOdtOOeXByvcrz5CAY2lqWByMXAXl4oR95JYRk3cJuVaCT9v5X6ZREB6gej9f869aCcLjmOGImt84kIZlGBeaNdl9-DKHDxQaOhI6H6TGqyDxB6Kiy7zKxhv7mQ"
+
+	cfg, _ := oidc.Configure("https://sso.provi.de/application/o/my-cool-app/", "")
+
+	// no credentials needed
+
+	tok, _ := cfg.ParseJWT(token)
+
+	groupsAny, _ := tok.GetField("groups")
+	groups := groupsAny.([]string)
+
+	// Output: admin users vpn
+	fmt.Println(strings.Join(groups, " "))
+}