-
-
Notifications
You must be signed in to change notification settings - Fork 107
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
(add) support for managing multiple API keys with scope permissions
- Loading branch information
Showing
10 changed files
with
197 additions
and
98 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,48 +1,46 @@ | ||
from karrio.server.serializers import ( | ||
owned_model_serializer, | ||
Serializer, | ||
Context, | ||
) | ||
from karrio.server.user.models import Token | ||
|
||
|
||
@owned_model_serializer | ||
class TokenSerializer(Serializer): | ||
def create(self, validated_data: dict, context: Context) -> Token: | ||
extra = ( | ||
dict(org__id=getattr(context.org, "id", None)) | ||
if hasattr(Token, "org") | ||
else {} | ||
) | ||
token = Token.objects.filter( | ||
user=context.user, | ||
test_mode=context.test_mode, | ||
**extra, | ||
).first() | ||
import karrio.server.conf as conf | ||
import karrio.server.user.models as models | ||
import karrio.server.serializers as serializers | ||
|
||
if token: | ||
return token | ||
|
||
return Token.objects.create(user=context.user, test_mode=context.test_mode) | ||
@serializers.owned_model_serializer | ||
class TokenSerializer(serializers.Serializer): | ||
label = serializers.CharField(required=False) | ||
|
||
def create( | ||
self, validated_data: dict, context: serializers.Context | ||
) -> models.Token: | ||
return models.Token.objects.create( | ||
user=context.user, | ||
test_mode=context.test_mode, | ||
label=validated_data.get("label") or "Default API Key", | ||
) | ||
|
||
@staticmethod | ||
def retrieve_token(context, org_id: str = None): | ||
user = getattr(context, "user", None) | ||
test_mode = getattr(context, "test_mode", None) | ||
org = getattr(context, "org", None) | ||
org_id = org_id or getattr(org, "id", None) | ||
|
||
queyset = models.Token.objects.filter( | ||
**{ | ||
"test_mode": getattr(context, "test_mode", None), | ||
"user__id": getattr(getattr(context, "user", None), "id", None), | ||
**({"org__id": org_id} if org_id is not None else {}), | ||
} | ||
) | ||
|
||
if org_id is not None and hasattr(Token, "org"): | ||
import karrio.server.orgs.models as orgs | ||
if queyset.exists(): | ||
return queyset.first() | ||
|
||
org = orgs.Organization.objects.get( | ||
id=org_id, users__id=getattr(user, "id", None) | ||
) | ||
else: | ||
org = getattr(context, "org", None) | ||
if org_id is not None and conf.settings.MULTI_ORGANIZATIONS: | ||
import karrio.server.orgs.models as orgs | ||
|
||
ctx = Context(user, org, test_mode) | ||
tokens = Token.access_by(ctx).filter(user__id=getattr(user, "id", None)) | ||
org = orgs.Organization.objects.get(id=org_id, users__id=context.user.id) | ||
|
||
if tokens.exists(): | ||
return tokens.first() | ||
ctx = serializers.Context( | ||
org=org, | ||
user=getattr(context, "user", None), | ||
test_mode=getattr(context, "test_mode", None), | ||
) | ||
|
||
return TokenSerializer.map(data={}, context=ctx).save().instance |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.