11import { randomBytes } from 'crypto' ;
22import { config , graphql } from '@keystone-6/core' ;
3- import type { SessionStrategy } from '@keystone-6/core/src/types/session' ;
4- import { Context } from '.keystone/types' ;
53import { lists } from './schema' ;
4+ import { Context , TypeInfo } from '.keystone/types' ;
65
7- type Session = {
8- id : string
9- data : {
10- id : string
11- }
12- } ;
13-
14- function mySessionStrategy ( ) : SessionStrategy < Session , {
15- id : string
16- } > {
17- return {
18- async start ( { data : { id } , createContext } ) { // TODO: change the return type of this to unknown/T
19- const sudoContext = createContext ( { } ) . sudo ( ) ;
20- const token = randomBytes ( 16 ) . toString ( 'hex' ) ; // random 128-bit token
21-
22- await sudoContext . db . Session . createOne ( {
23- data : {
24- token,
25- user : { connect : { id } } ,
26- ended : false
27- } ,
28- } ) ;
6+ async function startSession ( { id, context } : { id : string ; context : Context } ) {
7+ const sudoContext = context . sudo ( ) ;
8+ const token = randomBytes ( 16 ) . toString ( 'hex' ) ; // random 128-bit token
299
30- return token ;
10+ await sudoContext . db . Session . createOne ( {
11+ data : {
12+ token,
13+ user : { connect : { id } } ,
14+ ended : false ,
3115 } ,
16+ } ) ;
3217
33- // this populates the session object
34- async get ( { req, createContext } ) {
35- const sudoContext = createContext ( { } ) . sudo ( ) ;
36- const token = req . headers ?. authorization ;
37- if ( ! token ) return ; // not authenticated
38- // TODO: hash the token for timing attack
39-
40- const item = await sudoContext . query . Session . findOne ( {
41- where : {
42- token
43- } ,
44- query : 'user { id } ended' ,
45- } ) ;
18+ return token ;
19+ }
20+ async function endSession ( { context } : { context : Context } ) {
21+ const sudoContext = context . sudo ( ) ;
22+ const token = context . req ?. headers ?. authorization ;
23+ if ( ! token ) return ; // not authenticated
24+
25+ await sudoContext . db . Session . updateOne ( {
26+ where : {
27+ token,
28+ } ,
29+ data : {
30+ ended : true ,
31+ } ,
32+ } ) ;
33+ }
4634
47- // no session
48- if ( ! item ) return ;
35+ async function getSession ( { context } : { context : Context } ) {
36+ const sudoContext = context . sudo ( ) ;
37+ const token = context . req ?. headers ?. authorization ;
38+ if ( ! token ) return ; // not authenticated
4939
50- const { user, ended } = item ;
51- if ( ! user ) return ; // uh, shouldnt happen
40+ const item = await sudoContext . query . Session . findOne ( {
41+ where : {
42+ token,
43+ } ,
44+ query : 'user { id } ended' ,
45+ } ) ;
5246
53- // is it still active?
54- if ( ended ) return ;
47+ // no session
48+ if ( ! item ) return ;
5549
56- // they have a session
57- return {
58- id : user . id ,
59- data : {
60- id : user . id
61- }
62- } ;
63- } ,
50+ const { user, ended } = item ;
51+ if ( ! user ) return ; // uh, shouldnt happen
6452
65- async end ( { req, createContext } ) {
66- const sudoContext = createContext ( { } ) . sudo ( ) ;
67- const token = req . headers ?. authorization ;
68- if ( ! token ) return ; // not authenticated
53+ // is it still active?
54+ if ( ended ) return ;
6955
70- await sudoContext . db . Session . updateOne ( {
71- where : {
72- token
73- } ,
74- data : {
75- ended : true
76- } ,
77- } ) ;
56+ // they have a session
57+ return {
58+ id : user . id ,
59+ data : {
60+ id : user . id ,
7861 } ,
7962 } ;
8063}
8164
82- export const extendGraphqlSchema = graphql . extend ( ( base ) => {
65+ export const extendGraphqlSchema = graphql . extend ( base => {
8366 return {
8467 mutation : {
8568 authenticate : graphql . field ( {
8669 args : {
8770 id : graphql . arg ( { type : graphql . nonNull ( graphql . ID ) } ) ,
8871 } , // parameters
8972 type : base . object ( 'Session' ) , // return type
90- async resolve ( source , { id } , context ) {
91- const token = await context . startSession ( { id } ) ; // TODO: should be an object
92- console . log ( { token } )
93- return { } ;
73+ async resolve ( source , { id } , context : Context ) {
74+ const token = await startSession ( { id, context } ) ;
75+ console . log ( { token } ) ;
76+ return { token } ;
9477 } ,
9578 } ) ,
9679
@@ -99,10 +82,10 @@ export const extendGraphqlSchema = graphql.extend((base) => {
9982 id : graphql . arg ( { type : graphql . nonNull ( graphql . ID ) } ) ,
10083 } , // parameters
10184 type : base . object ( 'Session' ) , // return type
102- async resolve ( source , { id } , context ) {
85+ async resolve ( source , { id } , context : Context ) {
10386 if ( ! context . session ) return { } ; // only authenticated peeps
10487
105- const token = await context . startSession ( { id } ) ; // TODO: should be an object
88+ const token = await startSession ( { id, context } ) ;
10689 return { id, token } ;
10790 } ,
10891 } ) ,
@@ -112,26 +95,25 @@ export const extendGraphqlSchema = graphql.extend((base) => {
11295 token : graphql . arg ( { type : graphql . nonNull ( graphql . String ) } ) ,
11396 } , // parameters
11497 type : base . object ( 'Session' ) , // return type
115- async resolve ( source , { token } , context ) {
116- await context . endSession ( { token } ) ; // TODO: should be an object
98+ async resolve ( source , args , context : Context ) {
99+ await endSession ( { context } ) ;
117100 } ,
118101 } ) ,
119102 } ,
120103 } ;
121104} ) ;
122105
123- async function insertSeedData ( context : Context ) {
106+ async function insertSeedData ( context : Context ) {
124107 const { id } = await context . db . User . createOne ( {
125108 data : {
126- name : 'Daniel'
109+ name : 'Daniel' ,
127110 } ,
128- query : 'id'
129111 } ) ;
130112
131113 console . error ( 'created user' , { id } ) ;
132114}
133115
134- export default config ( {
116+ export default config < TypeInfo > ( {
135117 db : {
136118 provider : 'sqlite' ,
137119 url : process . env . DATABASE_URL || 'file:./keystone-example.db' ,
@@ -142,7 +124,7 @@ export default config({
142124 } ,
143125 } ,
144126 lists,
145- session : mySessionStrategy ( ) ,
127+ getSession ,
146128 extendGraphqlSchema,
147129} ) ;
148130
0 commit comments