@@ -55,9 +55,6 @@ export default function adminRouter(dbController: DatabaseController): Router {
5555 router . get ( "/auth/cas-validate" , async ( req : Request , res : Response ) => {
5656 const { ticket, service } = req . query ;
5757
58- console . log ( "hit" ) ;
59- console . log ( ticket ) ;
60- console . log ( service ) ;
6158 if ( ! ticket || ! service ) {
6259 res . status ( 400 ) . json ( { error : "Missing ticket or service" } ) ;
6360 }
@@ -74,25 +71,27 @@ export default function adminRouter(dbController: DatabaseController): Router {
7471
7572
7673 const casData = casResponse . data ; // assumed user CAS info, need to test to see
77- const nsid = casData . user ; // Potentally the nsid of the user. Again need to test
78-
79- console . log ( casData ) ;
80- console . log ( nsid ) ;
81- if ( ! nsid ) {
82- res . status ( 401 ) . json ( { error : "Invalid CAS Ticket" } ) ;
83- }
84-
85- if ( ! allowedNSIDs . includes ( nsid ) ) {
86- res . status ( 403 ) . json ( { error : "Access denied" } ) ;
87- }
88-
89- const token = jwt . sign (
90- { username : casData . user , roles : casData . roles } ,
91- process . env . JWT_SECRET ! ,
92- { expiresIn : "3h" } ,
93- ) ;
94-
95- res . json ( { token } ) ;
74+ if ( casData . includes ( '<cas:authenticationSuccess>' ) ) {
75+ const nsid = casData . match ( / < c a s : u s e r > ( .* ?) < \/ c a s : u s e r > / ) [ 1 ] ;
76+ console . log ( `User logged in with nsid: ${ nsid } ` ) ;
77+ if ( ! nsid ) {
78+ res . status ( 401 ) . json ( { error : "Invalid CAS Ticket" } ) ;
79+ }
80+
81+ if ( ! allowedNSIDs . includes ( nsid ) ) {
82+ console . log ( `User attempted to login with nsid: ${ nsid } ` ) ;
83+ res . status ( 403 ) . json ( { error : "Access denied" } ) ;
84+ }
85+ const token = jwt . sign (
86+ { username : casData . user , roles : casData . roles } ,
87+ process . env . JWT_SECRET ! ,
88+ { expiresIn : "3h" } ,
89+ ) ;
90+ res . json ( { token} ) ;
91+ } else {
92+ res . status ( 401 ) . json ( { error : 'CAS authentication failed' } ) ;
93+
94+ }
9695 } catch ( error ) {
9796 res . status ( 500 ) . json ( { error : "CAS validation failed" } ) ;
9897 }
0 commit comments