/**
 * Created by Omnius on 6/25/16.
 */
'use strict';

const Hawk = require('hawk');
const Joi = require('joi');
const retrieveFromToken = require(process.cwd() + '/server/helpers/hkdfTokenGenerator').retrieveFromToken;

module.exports = [
    {
        // Web login
        path: '/',
        method: '*',
        config: {
            handler: {
                webHandler: {
                    type: 'index'
                }
            }
        }
    },
    {
        // Web login
        path: '/login',
        method: '*',
        config: {
            handler: {
                webHandler: {
                    type: 'index'
                }
            }
        }
    },
    {
        // Web registration
        path: '/registration',
        method: '*',
        handler: {
            webHandler: {
                type: 'registration'
            }
        }
    },
    {
        // Web welcome page via hawk
        path: '/user/welcomeh',
        method: 'GET',
        config: {
            auth: {
                mode: 'required',
                strategies: ['hawk-login-auth-strategy']
            },
            cache: false,
            ext: {
                onPreAuth: {
                    method: (request, reply) => {

                        const clientToken = request.state['Hawk-Session-Token'];

                        if (!(clientToken && clientToken.hawkSessionToken)) {
                            return reply.continue();
                        }

                        const server = request.server;

                        const ikm = clientToken.hawkSessionToken;
                        const info = request.info.host + '/hawkSessionToken';
                        const salt = '';
                        const length = 2 * 32;

                        server.log(ikm + ' ' + info + ' ' + salt + ' ' + length);

                        retrieveFromToken(ikm, info, salt, length, (id, key) => {
                            if (!(id && key)) {
                                return reply.continue();
                            }

                            const algorithm = clientToken.algorithm;

                            const hawkCredentials = {
                                id: id,
                                key: key,
                                algorithm: algorithm
                            };

                            const url = request.connection.info.protocol
                                + '://'
                                + request.info.host + '/user/welcomeh';

                            request.raw.req.url = url;

                            const header = Hawk.client.header(url,
                                'GET',
                                {credentials: hawkCredentials, ext: 'some-app-data'});

                            request.raw.req.headers.authorization = header.field;

                            return reply.continue();
                        });
                    }
                }
            },
            handler: {
                loginUserHandler: {
                    type: 'login'
                }
            }
        }
    },
    {
        // Web welcome page via basic
        path: '/user/welcomeb',
        method: 'GET',
        config: {
            auth: {
                mode: 'required',
                strategies: ['basic-login-auth-strategy']
            },
            cache: false,
            handler: {
                loginUserHandler: {
                    type: 'login'
                }
            }
        }
    },
    {
        path: '/user/logout',
        method: 'GET',
        config: {
            handler: {
                logoutUserHandler: {
                    type: 'logout'
                }
            }
        }
    },
    {
        path: '/user/registration',
        method: 'POST',
        config: {
            handler: {
                registerUserHandler: {
                    type: 'register'
                }
            },
            validate: {
                payload: {
                    username: Joi.string().required().min(3).max(20).regex(/^[a-zA-Z0-9]/),
                    email: Joi.string().required().email(),
                    password: Joi.string().required().min(8).max(20).regex(/^([a-zA-Z0-9@*#]{8,15})$/),
                    firstname: Joi.string().required().min(2).max(20).regex(/^[a-zA-Z0-9]/),
                    surname: Joi.string().required().min(2).max(20).regex(/^[a-zA-Z0-9]/),
                    birthdate: Joi.date().required().iso().format('YYYY-MM-DD').min('1900-01-01').max('2013-12-31'),
                    realm: Joi.string().regex(/^([a-zA-Z0-9@*#\s]{0,15})$/)
                }
            }
        }
    }
];

@@ -55,20 +55,27 @@
 %0A%0Aconst 
-H
+h
 awk
+PreAuth
  = requi
@@ -82,149 +82,75 @@
 re('
-hawk');%0Aconst Joi = require('joi');%0Aconst retrieveFromToken = require(process.cwd() + '/server/helpers/hkdfTokenGenerator').retrieveFromToken
+./preauth/hawkPreAuth');%0Aconst schema = require('./schemas/schema')
 ;%0A%0Am
@@ -1161,1773 +1161,19 @@
 od: 
-(request, reply) =%3E %7B%0A%0A                        const clientToken = request.state%5B'Hawk-Session-Token'%5D;%0A%0A                        if (!(clientToken && clientToken.hawkSessionToken)) %7B%0A                            return reply.continue();%0A                        %7D%0A%0A                        const server = request.server;%0A%0A                        const ikm = clientToken.hawkSessionToken;%0A                        const info = request.info.host + '/hawkSessionToken';%0A                        const salt = '';%0A                        const length = 2 * 32;%0A%0A                        server.log(ikm + ' ' + info + ' ' + salt + ' ' + length);%0A%0A                        retrieveFromToken(ikm, info, salt, length, (id, key) =%3E %7B%0A                            if (!(id && key)) %7B%0A                                return reply.continue();%0A                            %7D%0A%0A                            const algorithm = clientToken.algorithm;%0A%0A                            const hawkCredentials = %7B%0A                                id: id,%0A                                key: key,%0A                                algorithm: algorithm%0A                            %7D;%0A%0A                            const url = request.connection.info.protocol%0A                                + '://'%0A                                + request.info.host + '/user/welcomeh';%0A%0A                            request.raw.req.url = url;%0A%0A                            const header = Hawk.client.header(url,%0A                                'GET',%0A                                %7Bcredentials: hawkCredentials, ext: 'some-app-data'%7D);%0A%0A                            request.raw.req.headers.authorization = header.field;%0A%0A                            return reply.continue();%0A                        %7D);%0A                    %7D
+hawkPreAuth
 %0A   
@@ -2278,68 +2278,23 @@
 me: 
-Joi.string().required().min(3).max(20).regex(/%5E%5Ba-zA-Z0-9%5D/)
+schema.username
 ,%0A  
@@ -2322,39 +2322,20 @@
 il: 
-Joi.string().required()
+schema
 .email
-()
 ,%0A  
@@ -2366,458 +2366,207 @@
 rd: 
-Joi.string().required().min(8).max(20).regex(/%5E(%5Ba-zA-Z0-9@*#%5D%7B8,15%7D)$/),%0A                    firstname: Joi.string().required().min(2).max(20).regex(/%5E%5Ba-zA-Z0-9%5D/),%0A                    surname: Joi.string().required().min(2).max(20).regex(/%5E%5Ba-zA-Z0-9%5D/),%0A                    birthdate: Joi.date().required().iso().format('YYYY-MM-DD').min('1900-01-01').max('2013-12-31'),%0A                    realm: Joi.string().regex(/%5E(%5Ba-zA-Z0-9@*#%5Cs%5D%7B0,15%7D)$/)
+schema.password,%0A                    firstname: schema.firstname,%0A                    surname: schema.surname,%0A                    birthdate: schema.birthdate,%0A                    realm: schema.realm
 %0A   

Remove transferred codes regarding preauth and schema from route file