-
Notifications
You must be signed in to change notification settings - Fork 71
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
518 additions
and
15 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
$version: "2" | ||
|
||
namespace smithy4s.sandbox.oauth | ||
|
||
@tokenExchange | ||
service TokenApi { | ||
operations: [ | ||
CreateAccessToken | ||
] | ||
errors: [ | ||
BadRequest | ||
] | ||
} | ||
|
||
@http(method: "POST", uri: "/token") | ||
operation CreateAccessToken { | ||
input := { | ||
@required | ||
@xmlName("client_id") | ||
clientId: ClientId | ||
@required | ||
@xmlName("client_secret") | ||
clientSecret: ClientSecret | ||
@required | ||
@xmlName("grant_type") | ||
grantType: GrantType | ||
@required | ||
@xmlName("refresh_token") | ||
refreshToken: RefreshToken | ||
} | ||
output := { | ||
@required | ||
@jsonName("access_token") | ||
accessToken: AccessToken | ||
@required | ||
@jsonName("expires_in") | ||
expiresIn: ExpiresIn | ||
@required | ||
@jsonName("refresh_token") | ||
refreshToken: RefreshToken | ||
@required | ||
@jsonName("token_type") | ||
tokenType: TokenType | ||
} | ||
} | ||
|
||
enum GrantType { | ||
REFRESH_TOKEN = "refresh_token" | ||
} | ||
|
||
string ClientId | ||
|
||
string ClientSecret | ||
|
||
long ExpiresIn | ||
|
||
string AccessToken | ||
|
||
string RefreshToken | ||
|
||
enum TokenType { | ||
BEARER = "Bearer" | ||
} | ||
|
||
@error("client") | ||
@httpError(400) | ||
structure BadRequest { | ||
@required | ||
error: Error | ||
} | ||
|
||
enum Error { | ||
INVALID_CLIENT = "invalid_client" | ||
INVALID_GRANT = "invalid_grant" | ||
UNAUTHORIZED_CLIENT = "unauthorized_client" | ||
UNSUPPORTED_GRANT_TYPE = "unsupported_grant_type" | ||
} |
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 |
---|---|---|
@@ -0,0 +1,29 @@ | ||
$version: "2" | ||
|
||
namespace smithy4s.sandbox.oauth | ||
|
||
// TODO: Move out of sandbox. | ||
|
||
@protocolDefinition | ||
@trait(selector: "service") | ||
structure tokenExchange {} | ||
|
||
// TODO: Make UrlFormSchemaVisitors work with this. | ||
|
||
/// Unwraps the values of a list, set, or map into the containing / | ||
//structure/union. | ||
@trait( | ||
selector: ":is(structure, union) > :test(member > :test(list, map))", | ||
breakingChanges: [{change: "any"}] | ||
) | ||
structure queryFlattened {} | ||
|
||
// TODO: Make UrlFormSchemaVisitors work with this. | ||
|
||
/// The queryName trait allows a serialized form key to differ from a structure | ||
/// member name used in the model. | ||
@trait( | ||
selector: ":is(structure, union) > member", | ||
breakingChanges: [{change: "any"}] | ||
) | ||
string queryName |
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 |
---|---|---|
@@ -0,0 +1,72 @@ | ||
/* | ||
* Copyright 2021-2022 Disney Streaming | ||
* | ||
* Licensed under the Tomorrow Open Source Technology License, Version 1.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://disneystreaming.github.io/TOST-1.0.txt | ||
* | ||
* 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 smithy4s | ||
package sandbox | ||
package oauth | ||
|
||
import io.circe.Json | ||
import org.http4s.* | ||
import org.http4s.client.middleware.Logger | ||
import org.http4s.circe.CirceEntityDecoder._ | ||
import org.http4s.client.dsl.Http4sClientDsl | ||
import org.http4s.dsl.io._ | ||
import org.http4s.ember.client.EmberClientBuilder | ||
import zio.* | ||
import zio.interop.catz.* | ||
|
||
object ReferenceClient extends ZIOAppDefault with Http4sClientDsl[Task]: | ||
|
||
override def run: RIO[Scope, Unit] = for | ||
portString <- Console.readLine("Enter port: ") | ||
port <- ZIO | ||
.fromOption(portString.toIntOption) | ||
.mapError(_ => Exception(s"$portString is not a valid port.")) | ||
client <- EmberClientBuilder | ||
.default[Task] | ||
.build | ||
.map( | ||
Logger.colored( | ||
logHeaders = true, | ||
logBody = true, | ||
logAction = Some(Console.printLine(_)) | ||
) | ||
) | ||
.toScopedZIO | ||
oauthSandboxApiUri = Uri( | ||
scheme = Some(Uri.Scheme.http), | ||
authority = Some(Uri.Authority(port = Some(port))) | ||
) | ||
_ <- (for | ||
createAccessTokenOutput <- client | ||
.run( | ||
POST( | ||
UrlForm( | ||
"client_id" -> expectedClientId.value, | ||
"client_secret" -> expectedClientSecret.value, | ||
"grant_type" -> "refresh_token", | ||
"refresh_token" -> expectedRefreshToken.value | ||
), | ||
oauthSandboxApiUri / "token" | ||
) | ||
) | ||
.use(_.as[Json]) | ||
accessToken <- ZIO.fromEither( | ||
createAccessTokenOutput.hcursor.get[String]("access_token") | ||
) | ||
_ <- Console.printLine(s"Access token: $accessToken") | ||
yield ()).repeat(Schedule.spaced(1.second)) | ||
yield () |
Oops, something went wrong.