-
Notifications
You must be signed in to change notification settings - Fork 2
/
GoogleCredentials.scala
152 lines (136 loc) · 4.8 KB
/
GoogleCredentials.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/*
* Copyright 2021 Arman Bilge
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package gcp4s
package auth
import cats.data.NonEmptyList
import cats.data.OptionT
import cats.effect.kernel.Clock
import cats.effect.kernel.Concurrent
import cats.effect.kernel.MonadCancelThrow
import cats.effect.std.Env
import cats.effect.syntax.all.*
import cats.syntax.all.*
import fs2.CompositeFailure
import fs2.io.file.Files
import fs2.io.file.Path
import fs2.text
import io.circe.Decoder
import io.circe.jawn
import org.http4s.AuthScheme
import org.http4s.Credentials
import org.http4s.client.Client
import org.http4s.client.Middleware
import org.http4s.headers.Authorization
import scodec.bits.ByteVector
trait GoogleCredentials[F[_]]:
def projectId: String
def get: F[Credentials]
def middleware(using MonadCancelThrow[F]): Middleware[F] =
client =>
Client { req =>
for
creds <- get.toResource
res <- client.run(req.putHeaders(Authorization(creds)))
yield res
}
object ApplicationDefaultCredentials:
def apply[F[_]: Clock: Env: Files: Jwt](client: Client[F], scopes: Seq[String])(
using F: Concurrent[F]): F[GoogleCredentials[F]] =
val serviceAccountCredentials =
for
credentialsPath <- getWellKnownCredentials
json <- Files[F].readUtf8(credentialsPath).compile.string
ServiceAccountCredentialsFile(projectId, clientEmail, privateKey) <- jawn
.decode[ServiceAccountCredentialsFile](json)
.liftTo[F]
privateKey <- ByteVector.encodeAscii(privateKey).liftTo[F]
credentials <- ServiceAccountCredentials(
GoogleOAuth2(client),
projectId,
clientEmail,
privateKey,
scopes)
yield credentials
val computeEngineCredentials = ComputeEngineCredentials(ComputeMetadata(client))
serviceAccountCredentials.handleErrorWith { e1 =>
computeEngineCredentials.handleErrorWith { e2 =>
F.raiseError(CompositeFailure(e1, NonEmptyList.one(e2)))
}
}
object ServiceAccountCredentials:
def apply[F[_]: Concurrent: Clock](
oauth2: GoogleOAuth2[F],
projectId: String,
clientEmail: String,
privateKey: ByteVector,
scopes: Seq[String]): F[GoogleCredentials[F]] =
OAuth2Credentials(projectId, oauth2.getAccessToken(clientEmail, privateKey, scopes))
def apply[F[_]: Concurrent: Clock](
oauth2: GoogleOAuth2[F],
projectId: String,
clientEmail: String,
privateKey: String,
scopes: Seq[String]): F[GoogleCredentials[F]] =
for
privateKey <- parseKey(privateKey).liftTo[F]
credentials <- OAuth2Credentials(
projectId,
oauth2.getAccessToken(clientEmail, privateKey, scopes))
yield credentials
private def parseKey(key: String): Either[Throwable, ByteVector] =
ByteVector
.fromBase64Descriptive(
key
.replaceAll("-----BEGIN (.*)-----", "")
.replaceAll("-----END (.*)-----", "")
.replaceAll("\r\n", "")
.replaceAll("\n", "")
.trim)
.leftMap(new RuntimeException(_))
final case class ServiceAccountCredentialsFile(
project_id: String,
client_email: String,
private_key: String)
derives Decoder
object ComputeEngineCredentials:
def apply[F[_]: Concurrent: Clock](metadata: ComputeMetadata[F]): F[GoogleCredentials[F]] =
for
projectId <- metadata.getProjectId
credentials <- OAuth2Credentials(projectId, metadata.getAccessToken)
yield credentials
object OAuth2Credentials:
private[auth] def apply[F[_]: Clock](pid: String, refresh: F[AccessToken])(
using F: Concurrent[F]): F[GoogleCredentials[F]] =
for token <- F.ref(Option.empty[F[AccessToken]])
yield new GoogleCredentials[F]:
val projectId = pid
def get = for AccessToken(token, _) <- getToken
yield Credentials.Token(AuthScheme.Bearer, token)
def getToken: F[AccessToken] = OptionT(token.get)
.semiflatMap(identity)
.flatMapF { token =>
for expired <- token.expiresSoon()
yield Option.unless(expired)(token)
}
.getOrElseF {
for
memo <- F.memoize(refresh)
updated <- token.tryUpdate(_ => Some(memo))
token <-
if updated then memo
else getToken
yield token
}