|
1 | 1 | """Helpers for AIOGitHubAPI."""
|
2 | 2 | from __future__ import annotations
|
3 | 3 |
|
| 4 | +from io import BytesIO |
4 | 5 | from typing import Optional
|
5 | 6 |
|
6 | 7 | import aiohttp
|
| 8 | +from sigstore.verify import ( |
| 9 | + VerificationMaterials, |
| 10 | + VerificationResult, |
| 11 | + Verifier, |
| 12 | + models, |
| 13 | + policy, |
| 14 | +) |
7 | 15 |
|
8 | 16 | from .const import HttpMethod, Repository, RepositoryType
|
9 | 17 | from .legacy.helpers import (
|
@@ -37,3 +45,40 @@ async def async_call_api(
|
37 | 45 | return await legacy_async_call_api(
|
38 | 46 | session, method, url, headers, params, data, jsondata, returnjson
|
39 | 47 | )
|
| 48 | + |
| 49 | + |
| 50 | +def sigstore_verify_release_asset( |
| 51 | + asset: bytes, |
| 52 | + signature_bundle: bytes, |
| 53 | + repository: str, |
| 54 | + workflow: str, |
| 55 | + tag: str, |
| 56 | + *, |
| 57 | + workflow_name: str | None = None, |
| 58 | + workflow_trigger: str | None = None, |
| 59 | + offline_verification: bool = False, |
| 60 | + **kwargs, |
| 61 | +) -> VerificationResult: |
| 62 | + """Verify release asset.""" |
| 63 | + verifier = Verifier.production() |
| 64 | + policies = [ |
| 65 | + policy.Identity( |
| 66 | + identity=f"https://github.com/{repository}/.github/workflows/{workflow}@refs/tags/{tag}", |
| 67 | + issuer="https://token.actions.githubusercontent.com", |
| 68 | + ), |
| 69 | + policy.GitHubWorkflowRepository(repository), |
| 70 | + policy.GitHubWorkflowRef(f"refs/tags/{tag}"), |
| 71 | + ] |
| 72 | + if workflow_trigger: |
| 73 | + policies.append(policy.GitHubWorkflowTrigger(workflow_trigger)) |
| 74 | + if workflow_name: |
| 75 | + policies.append(policy.GitHubWorkflowName(workflow_name)) |
| 76 | + |
| 77 | + return verifier.verify( |
| 78 | + VerificationMaterials.from_bundle( |
| 79 | + input_=BytesIO(asset), |
| 80 | + bundle=models.Bundle().from_json(signature_bundle), |
| 81 | + offline=offline_verification, |
| 82 | + ), |
| 83 | + policy=policy.AllOf(policies), |
| 84 | + ) |
0 commit comments