Skip to content

Commit 645212b

Browse files
committed
scrub secrets and ask for less user info
1 parent 3ce672d commit 645212b

35 files changed

+483
-90
lines changed

Sources/GitHubClient/GitHubApplication.Client.swift renamed to Sources/GitHubClient/GitHubClient.swift

Lines changed: 41 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,35 +4,51 @@ import JSON
44
import NIOCore
55
import NIOHPACK
66

7-
extension GitHubApplication
7+
@frozen public
8+
struct GitHubClient<Application> where Application:GitHubApplication
89
{
9-
@frozen public
10-
struct Client
11-
{
12-
private
13-
let http2:ClientInterface
14-
15-
public
16-
let app:GitHubApplication
10+
private
11+
let http2:HTTP2Client
12+
public
13+
let app:Application
1714

18-
public
19-
init(http2:ClientInterface, app:GitHubApplication)
20-
{
21-
self.http2 = http2
22-
self.app = app
23-
}
15+
public
16+
init(http2:HTTP2Client, app:Application)
17+
{
18+
self.http2 = http2
19+
self.app = app
2420
}
2521
}
26-
extension GitHubApplication.Client:Identifiable
22+
extension GitHubClient:Identifiable
2723
{
2824
@inlinable public
2925
var id:String { self.app.client }
3026

3127
@inlinable public
3228
var secret:String { self.app.secret }
3329
}
34-
extension GitHubApplication.Client
30+
extension GitHubClient
3531
{
32+
public
33+
func refresh(token:String) async -> Result<GitHubTokens, GitHubAuthenticationError>
34+
{
35+
let request:HPACKHeaders =
36+
[
37+
":method": "POST",
38+
":scheme": "https",
39+
":authority": "github.com",
40+
":path": """
41+
/login/oauth/access_token?\
42+
grant_type=refresh_token&\
43+
client_id=\(self.id)&client_secret=\(self.secret)&refresh_token=\(token)
44+
""",
45+
46+
"accept": "application/vnd.github+json",
47+
]
48+
49+
return await self.authenticate(sending: request)
50+
}
51+
3652
public
3753
func exchange(code:String) async -> Result<GitHubTokens, GitHubAuthenticationError>
3854
{
@@ -49,7 +65,14 @@ extension GitHubApplication.Client
4965
"accept": "application/vnd.github+json",
5066
]
5167

52-
let response:ClientInterface.Facet
68+
return await self.authenticate(sending: request)
69+
}
70+
71+
private
72+
func authenticate(
73+
sending request:HPACKHeaders) async -> Result<GitHubTokens, GitHubAuthenticationError>
74+
{
75+
let response:HTTP2Client.Facet
5376
do
5477
{
5578
response = try await self.http2.fetch(request)

Sources/GitHubClient/exports.swift

Lines changed: 0 additions & 1 deletion
This file was deleted.
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/// The essence of a GitHub App, which is a type of GitHub application.
2+
@frozen public
3+
struct GitHubApp:GitHubApplication, Identifiable
4+
{
5+
/// The app id number. This is different from the client id.
6+
public
7+
let id:Int?
8+
9+
public
10+
let client:String
11+
public
12+
let secret:String
13+
14+
@inlinable public
15+
init(_ id:Int?, client:String, secret:String)
16+
{
17+
self.id = id
18+
self.client = client
19+
self.secret = secret
20+
}
21+
}
Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,7 @@
1-
@frozen public
2-
struct GitHubApplication:Identifiable, Equatable, Hashable, Sendable
1+
/// The essence of a GitHub application. Not to be confused with ``GitHubApp``.
2+
public
3+
protocol GitHubApplication:Equatable, Hashable, Sendable
34
{
4-
public
5-
let id:Int
6-
7-
public
8-
let client:String
9-
public
10-
let secret:String
11-
12-
@inlinable public
13-
init(_ id:Int, client:String, secret:String)
14-
{
15-
self.id = id
16-
self.client = client
17-
self.secret = secret
18-
}
5+
var client:String { get }
6+
var secret:String { get }
197
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
@frozen public
2+
struct GitHubCredential<Instant>:Sendable where Instant:Sendable
3+
{
4+
public
5+
let expires:Instant
6+
public
7+
let token:String
8+
9+
@inlinable public
10+
init(expires:Instant, token:String)
11+
{
12+
self.expires = expires
13+
self.token = token
14+
}
15+
}
16+
extension GitHubCredential:Equatable where Instant:Equatable
17+
{
18+
}
19+
extension GitHubCredential:Hashable where Instant:Hashable
20+
{
21+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/// The essence of a GitHub OAuth application.
2+
@frozen public
3+
struct GitHubOAuth:GitHubApplication
4+
{
5+
public
6+
let client:String
7+
public
8+
let secret:String
9+
10+
@inlinable public
11+
init(client:String, secret:String)
12+
{
13+
self.client = client
14+
self.secret = secret
15+
}
16+
}

Sources/GitHubIntegration/GitHubToken.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
@frozen public
22
struct GitHubToken:Equatable, Hashable, Sendable
33
{
4-
public
5-
let secondsRemaining:Int
64
public
75
let value:String
6+
public
7+
let secondsRemaining:Int64
88

99
@inlinable public
10-
init(value:String, secondsRemaining:Int)
10+
init(value:String, secondsRemaining:Int64)
1111
{
1212
self.value = value
1313
self.secondsRemaining = secondsRemaining

Sources/HTTPClient/ClientInterfaceHandler.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,13 @@ extension ClientInterfaceHandler:ChannelOutboundHandler
2121
typealias OutboundOut = HTTP2Frame
2222
typealias OutboundIn =
2323
(
24-
owner:AsyncThrowingStream<ClientInterface.Facet, any Error>.Continuation,
24+
owner:AsyncThrowingStream<HTTP2Client.Facet, any Error>.Continuation,
2525
batch:[HPACKHeaders]
2626
)
2727

2828
func write(context:ChannelHandlerContext, data:NIOAny, promise:EventLoopPromise<Void>?)
2929
{
30-
let owner:AsyncThrowingStream<ClientInterface.Facet, any Error>.Continuation
30+
let owner:AsyncThrowingStream<HTTP2Client.Facet, any Error>.Continuation
3131
let batch:[HPACKHeaders]
3232

3333
(owner, batch) = self.unwrapOutboundIn(data)

Sources/HTTPClient/ClientStreamHandler.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ final
66
class ClientStreamHandler
77
{
88
private
9-
var owner:AsyncThrowingStream<ClientInterface.Facet, any Error>.Continuation?
9+
var owner:AsyncThrowingStream<HTTP2Client.Facet, any Error>.Continuation?
1010
private
11-
var facet:ClientInterface.Facet
11+
var facet:HTTP2Client.Facet
1212

13-
init(owner:AsyncThrowingStream<ClientInterface.Facet, any Error>.Continuation?)
13+
init(owner:AsyncThrowingStream<HTTP2Client.Facet, any Error>.Continuation?)
1414
{
1515
self.owner = owner
1616
self.facet = .init()

Sources/HTTPClient/ClientInterface.Facet.swift renamed to Sources/HTTPClient/HTTP2Client.Facet.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import NIOCore
22
import NIOHPACK
33
import NIOHTTP2
44

5-
extension ClientInterface
5+
extension HTTP2Client
66
{
77
@frozen public
88
struct Facet:Sendable
@@ -19,7 +19,7 @@ extension ClientInterface
1919
}
2020
}
2121
}
22-
extension ClientInterface.Facet
22+
extension HTTP2Client.Facet
2323
{
2424
/// Validates the payload and adds it to the facet. Returns true if the frame is the last
2525
/// frame of the response stream, false otherwise.
@@ -45,6 +45,6 @@ extension ClientInterface.Facet
4545
break
4646
}
4747

48-
throw ClientInterface.UnexpectedFrameError.init(payload)
48+
throw HTTP2Client.UnexpectedFrameError.init(payload)
4949
}
5050
}

0 commit comments

Comments
 (0)