File tree Expand file tree Collapse file tree 5 files changed +50
-12
lines changed Expand file tree Collapse file tree 5 files changed +50
-12
lines changed Original file line number Diff line number Diff line change 2
2
3
3
import { cookies as getCookies , headers as getHeaders } from "next/headers" ;
4
4
import { redirect } from "next/navigation" ;
5
- import { createAuthorizationUrl } from "./issuer" ;
5
+ import { createAuthorizationUrl , OIDC_ISSUER } from "./issuer" ;
6
+
7
+ export async function maybeStartAuthorizationAuto (
8
+ params : Record < string , string >
9
+ ) {
10
+ // See https://openid.net/specs/openid-connect-core-1_0.html#ThirdPartyInitiatedLogin
11
+ const { iss, login_hint, target_link_uri, v_deeplink } = params ;
12
+ if ( iss !== OIDC_ISSUER || ! login_hint ) {
13
+ return ;
14
+ }
15
+
16
+ const headers = getHeaders ( ) ;
17
+ const cookies = getCookies ( ) ;
18
+
19
+ const host = headers . get ( "host" ) ;
20
+
21
+ const protocol = host ?. startsWith ( "localhost" ) ? "http" : "https" ;
22
+ const callbackUrl = `${ protocol } ://${ host } /login/vercel/callback` ;
23
+
24
+ const { redirectTo, state } = await createAuthorizationUrl ( {
25
+ callbackUrl,
26
+ login_hint,
27
+ v_deeplink,
28
+ } ) ;
29
+
30
+ cookies . set ( "vercel-oidc-state" , state , { httpOnly : true } ) ;
31
+ return redirect ( redirectTo ) ;
32
+ }
6
33
7
34
export async function startAuthorization ( formData : FormData ) {
8
35
console . log ( "startAuthorization:" , Object . fromEntries ( formData ) ) ;
Original file line number Diff line number Diff line change @@ -4,6 +4,9 @@ import { getTokens } from "../issuer";
4
4
export async function GET ( req : Request ) {
5
5
const url = req . url ;
6
6
7
+ const params = Object . fromEntries ( new URL ( url ) . searchParams ) ;
8
+ const { v_deeplink } = params ;
9
+
7
10
const cookies = await getCookies ( ) ;
8
11
const expectedState = cookies . get ( "vercel-oidc-state" ) ?. value || undefined ;
9
12
console . log ( "Callback:" , { url, expectedState } ) ;
@@ -14,6 +17,6 @@ export async function GET(req: Request) {
14
17
cookies . set ( "id-token" , id_token ) ;
15
18
}
16
19
17
- // TODO: redirect to the /dashboard
18
- return Response . json ( { id_token, claims } ) ;
20
+ // TODO: redirect to the /dashboard based on v_deeplink.
21
+ return Response . json ( { id_token, claims, v_deeplink } ) ;
19
22
}
Original file line number Diff line number Diff line change @@ -11,7 +11,7 @@ import {
11
11
} from "openid-client" ;
12
12
import { createRemoteJWKSet , jwtVerify } from "jose" ;
13
13
14
- const OIDC_ISSUER = "http://localhost:4000" ;
14
+ export const OIDC_ISSUER = "http://localhost:4000" ;
15
15
const OIDC_CLIENT_ID = "my_web_app1" ;
16
16
const OIDC_CLIENT_SECRET = "super_secret_client_secret1" ;
17
17
@@ -53,9 +53,13 @@ export async function getOidcConfiguration(): Promise<Configuration> {
53
53
54
54
export async function createAuthorizationUrl ( {
55
55
callbackUrl,
56
+ login_hint,
57
+ v_deeplink,
56
58
explicit = true ,
57
59
} : {
58
60
callbackUrl : string ;
61
+ login_hint ?: string ;
62
+ v_deeplink ?: string ;
59
63
explicit ?: boolean ;
60
64
} ) : Promise < {
61
65
redirectTo : string ;
@@ -70,6 +74,8 @@ export async function createAuthorizationUrl({
70
74
scope : "openid" ,
71
75
state,
72
76
response_type : explicit ? "code" : "id_token" ,
77
+ ...( login_hint ? { login_hint } : null ) ,
78
+ ...( v_deeplink ? { v_deeplink } : null ) ,
73
79
} ) ;
74
80
75
81
return {
Original file line number Diff line number Diff line change 1
- import { startAuthorization , startImplicitAuthorization } from "./actions" ;
2
-
3
- interface LoginSearchParams { }
4
-
5
- export default async function LoginVercelPage ( props : {
6
- searchParams : Promise < LoginSearchParams > ;
7
- } ) {
8
- const searchParams = await props . searchParams ;
1
+ import { startAuthorization , startImplicitAuthorization } from "../actions" ;
9
2
3
+ export default async function LoginVercelPage ( ) {
10
4
return (
11
5
< div className = "bg-gray-100 h-screen flex items-center justify-center" >
12
6
< div className = "bg-white shadow-md rounded px-8 pt-6 pb-8 mb-4 w-full max-w-sm" >
Original file line number Diff line number Diff line change
1
+ import { maybeStartAuthorizationAuto } from "./actions" ;
2
+
3
+ export async function GET ( req : Request ) {
4
+ const params = Object . fromEntries ( new URL ( req . url ) . searchParams ) ;
5
+ await maybeStartAuthorizationAuto ( params ) ;
6
+
7
+ return Response . redirect ( new URL ( "/login/vercel/prompt" , req . url ) , 307 ) ;
8
+ }
You can’t perform that action at this time.
0 commit comments