-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate_auth_url_example.py
More file actions
90 lines (77 loc) · 2.61 KB
/
generate_auth_url_example.py
File metadata and controls
90 lines (77 loc) · 2.61 KB
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
import argparse
import urllib.parse
def generate_auth_url(
client_id: str,
redirect_uri: str,
scopes: list[str],
state: str = "state123",
code_challenge: str = "challenge123",
code_challenge_method: str = "S256",
) -> str:
"""
Generate the Snyk OAuth2 authorization URL.
Args:
client_id (str): The client ID for the Snyk app.
redirect_uri (str): The redirect URI for the Snyk app.
scopes (list[str]): List of scopes for the Snyk app.
state (str): State parameter for CSRF protection.
code_challenge (str): PKCE code challenge.
code_challenge_method (str): PKCE code challenge method.
Returns:
str: The complete authorization URL.
"""
params: dict = {
"response_type": "code",
"client_id": client_id,
"redirect_uri": redirect_uri,
"scope": " ".join(scopes),
"state": state,
"code_challenge": code_challenge,
"code_challenge_method": code_challenge_method,
}
return "https://app.snyk.io/oauth2/authorize?" + urllib.parse.urlencode(params)
def main():
parser: argparse.ArgumentParser = argparse.ArgumentParser(
description="Generate a Snyk OAuth2 authorization URL."
)
parser.add_argument("--client-id", required=True, help="Snyk App client ID")
parser.add_argument(
"--redirect-uri",
default="https://example.com",
help="Redirect URI for the Snyk App",
)
parser.add_argument(
"--scopes",
default=[
"org.read",
"org.project.read",
"org.report.read",
"org.collection.read",
"org.project.snapshot.read",
],
nargs="+",
help="Scopes for the Snyk App (default: org.read org.project.read org.report.read org.collection.read org.project.snapshot.read). Separate multiple scopes with spaces.",
)
parser.add_argument(
"--state", default="state123", help="State parameter for CSRF protection"
)
parser.add_argument(
"--code-challenge", default="challenge123", help="PKCE code challenge"
)
parser.add_argument(
"--code-challenge-method",
default="S256",
help="PKCE code challenge method (default: S256)",
)
args: argparse.Namespace = parser.parse_args()
url: str = generate_auth_url(
client_id=args.client_id,
redirect_uri=args.redirect_uri,
scopes=args.scopes,
state=args.state,
code_challenge=args.code_challenge,
code_challenge_method=args.code_challenge_method,
)
print(url)
if __name__ == "__main__":
main()