Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ for the application.
environment.

```
pip install git+https://github.com/uw-ipd/git-credential-github-app-auth
pip install git+https://github.com/KPMG-UK/git-credential-github-app-auth
```

3. Add `github-app-auth` as a git-credential helper and ensure that
Expand Down
9 changes: 4 additions & 5 deletions git_credential_github_app_auth/cli.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import logging
import json
import sys

import click

Expand Down Expand Up @@ -66,16 +67,14 @@ def token(appidentity, account):
@click.command(help="Credential storage helper implementation.")
@pass_appidentity
@click.argument('input', type=click.File('r'), default="-")
@click.argument('output', type=click.File('w'), default="-")
def get(appidentity, input, output):
def get(appidentity, input):
# https://git-scm.com/docs/git-credential
logger.debug("get id: %s input: %s output: %s", appidentity, input, output)
logger.debug("get id: %s input: %s", appidentity, input)

def token_for_account(account):
return installation_token_for(account, appidentity)["token"]

output.write(credential_helper(input.read(), token_for_account))
output.write("\n")
sys.stdout.write(credential_helper(input.read(), token_for_account))


@cli.command(help="no-op git-credential interface")
Expand Down
24 changes: 14 additions & 10 deletions git_credential_github_app_auth/identity.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

logger = logging.getLogger(__name__)


@attr.s(frozen=True)
class AppIdentity:
"""Manages a github app id/key pair and signed token generation.
Expand All @@ -31,7 +32,9 @@ def _resolve_app_id(app_id: Optional[Union[int, str]] = None):

app_id = os.getenv(AppIdentity.APP_ID_ENV_VAR)
if app_id is None:
raise ValueError("Unable to resolve app_id from env: %s" % AppIdentity.APP_ID_ENV_VAR)
raise ValueError(
"Unable to resolve app_id from env: %s" % AppIdentity.APP_ID_ENV_VAR
)
logger.info("Resolved %s to app id: %s", AppIdentity.APP_ID_ENV_VAR, app_id)

try:
Expand All @@ -51,7 +54,10 @@ def _resolve_key(private_key: Optional[str] = None):
logger.debug("Resolving private_key from env.")
private_key = os.getenv(AppIdentity.PRIVATE_KEY_ENV_VAR)
if private_key is None:
raise ValueError("Unable to resolve private_key from env: %s" % AppIdentity.PRIVATE_KEY_ENV_VAR)
raise ValueError(
"Unable to resolve private_key from env: %s"
% AppIdentity.PRIVATE_KEY_ENV_VAR
)
logger.info("Resolved %s to private key.", AppIdentity.PRIVATE_KEY_ENV_VAR)

if "BEGIN RSA PRIVATE KEY" in private_key:
Expand All @@ -63,14 +69,14 @@ def _resolve_key(private_key: Optional[str] = None):
logger.info("Resolved private key.")
return private_key

app_id : int = attr.attrib(
app_id: int = attr.attrib(
converter=_resolve_app_id.__func__,
default = attr.Factory(lambda: AppIdentity._resolve_app_id())
default=attr.Factory(lambda: AppIdentity._resolve_app_id()),
)
private_key : str = attr.attrib(
private_key: str = attr.attrib(
repr=False,
converter=_resolve_key.__func__,
default = attr.Factory(lambda: AppIdentity._resolve_key())
default=attr.Factory(lambda: AppIdentity._resolve_key()),
)

def jwt(self) -> str:
Expand All @@ -81,11 +87,9 @@ def jwt(self) -> str:
"""
issue_time = time.time() - 1
payload = dict(
iat= int(issue_time),
exp= int(issue_time + (10 * 60)),
iss= self.app_id
iat=int(issue_time), exp=int(issue_time + (10 * 60)), iss=self.app_id
)

logging.debug("Issuing app jwt: %s", payload)

return jwt.encode(payload, self.private_key, algorithm='RS256').decode()
return jwt.encode(payload, self.private_key, algorithm="RS256")