Coverage for fastid / integrations / vk / oauth.py: 57%

24 statements  

« prev     ^ index     » next       coverage.py v7.13.1, created at 2026-10-18 16:37 +0000

1from typing import Any 

2 

3from fastid.auth.schemas import DiscoveryDocument, OAuth2Callback, OpenID, ProviderMeta 

4from fastid.integrations.base.oauth import OAuth2Client 

5from fastid.integrations.exceptions import UserinfoError 

6from fastid.integrations.schemas import UserinfoResponse 

7 

8 

9class VKSSO(OAuth2Client): 

10 default_use_pkce = True 

11 pkce_challenge_method = "s256" 

12 use_client_secret = False 

13 default_meta = ProviderMeta( 

14 name="vk", 

15 title="VK", 

16 discovery=DiscoveryDocument( 

17 authorization_endpoint="https://id.vk.com/authorize", 

18 token_endpoint="https://id.vk.com/oauth2/auth", # noqa: S106 

19 userinfo_endpoint="https://id.vk.com/oauth2/user_info", 

20 ), 

21 scope=["email"], 

22 ) 

23 

24 def _prepare_token_request( 

25 self, 

26 callback: OAuth2Callback, 

27 *, 

28 body: dict[str, Any] | None = None, 

29 headers: dict[str, str] | None = None, 

30 ) -> Any: 

31 body = body or {} 

32 if callback.device_id is not None: 32 ↛ 34line 32 didn't jump to line 34 because the condition on line 32 was always true

33 body["device_id"] = callback.device_id 

34 return super()._prepare_token_request(callback, body=body, headers=headers) 

35 

36 async def userinfo(self) -> UserinfoResponse: 

37 assert self.discovery.userinfo_endpoint is not None 

38 response = await self.client.post( 

39 self.discovery.userinfo_endpoint, 

40 headers={"Content-Type": "application/x-www-form-urlencoded"}, 

41 data={ 

42 "client_id": self.client_id, 

43 "access_token": self.token.access_token, 

44 }, 

45 ) 

46 content = response.json() 

47 user = content.get("user") 

48 if response.is_error or not user: 

49 msg = "Getting userinfo failed: %s" 

50 raise UserinfoError(msg, content) 

51 

52 userinfo = await self.convert_userinfo(user) 

53 return UserinfoResponse(userinfo=userinfo, userinfo_raw=content) 

54 

55 async def convert_userinfo(self, response: dict[str, Any]) -> OpenID: 

56 first_name = response.get("first_name") 

57 last_name = response.get("last_name") 

58 return OpenID( 

59 id=str(response["user_id"]), 

60 email=response.get("email"), 

61 first_name=first_name, 

62 last_name=last_name, 

63 display_name=" ".join(filter(None, (first_name, last_name))) or None, 

64 picture=response.get("avatar"), 

65 )