Skip to content

Commit e042903

Browse files
committed
Add from_url to Scopes
1 parent e5671ae commit e042903

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

twitchio/authentication/scopes.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -413,3 +413,33 @@ def selected(self) -> list[str]:
413413
def all(cls) -> Scopes:
414414
"""Classmethod which creates this :class:`.Scopes` object with all scopes selected."""
415415
return cls([scope for scope in cls.__dict__.values() if isinstance(scope, _scope_property)])
416+
417+
@classmethod
418+
def from_url(cls, url: str) -> Scopes:
419+
"""Classmethod which attempts to create this :class:`.Scopes` object from a URL containing scopes as a query parameter."""
420+
self = cls()
421+
422+
if not url.startswith("http"):
423+
url = url.replace("?scopes=", "")
424+
url = f"http://localhost:4343?scopes={url}"
425+
426+
parsed = urllib.parse.parse_qs(url)
427+
scopes: list[str] = []
428+
429+
for qs in parsed.values():
430+
for s in qs:
431+
unquoted = urllib.parse.unquote_plus(s)
432+
splat = unquoted.split()
433+
scopes.extend(splat)
434+
435+
for scope in scopes:
436+
if scope == "openid":
437+
continue
438+
439+
prop = getattr(self, scope.replace(":", "_"), None)
440+
if not prop:
441+
continue
442+
443+
self._selected.add(prop)
444+
445+
return self

0 commit comments

Comments
 (0)