]>
git.defcon.no Git - hermes/blob - api/lib/auth_base.php
2 require_once('config.php');
4 $config = get_config();
6 /*******************************
7 * Load authentication plugin ..
8 *******************************/
9 if ( preg_match('/^\w+$/', $config['auth_backend']))
11 if ( !@include_once
( 'lib/auth_plugins/' . $config['auth_backend'] . ".php" ) )
12 { print json_encode( array( 'response' => 'error', 'cause' => 'auth-load' ) ); exit; }
15 { print json_encode( array( 'response' => 'error', 'cause' => 'config-error' ) ); exit; }
16 /*******************************/
18 function apikey_verify( $key )
20 if ( $key == "6327c08b70f9" ) return 1;
24 function new_key( $hex = false )
26 // Basically this is at the moment a slightly modified
27 // version of generate_password() from user_functiions.php
28 // The behaviour/output of this function is expected to change
29 // so using generate_password() directly does not make sense...
32 while ( strlen( $string ) < $length )
35 $string .= substr(md5(rand().rand()), 0, $length);
38 $string .= crypt( substr(sha1(rand()), 0, $length) );
39 $string = preg_replace( '/\W/', '', $string);
42 return substr( $string, 0, $length );
45 function simple_authfail()
47 print json_encode( array( 'response' => 'failed', 'cause' => 'unauthorized', 'description' => 'Not authorized') );
51 function token_auth( )
55 // TODO: Part of ping/pong requirement.
56 // Run a function to clear all authkeys older than 5 minutes.
59 if ( array_key_exists('session', $_GET )
60 && array_key_exists('auth_key', $_GET ) )
62 if ( ! check_session($_GET['session'] ) ) simple_authfail();
63 if ( ! check_authkey($_GET['auth_key'] ) ) simple_authfail();
65 else simple_authfail();
68 function get_cookie_path ()
70 $name = $_SERVER["SCRIPT_NAME"];
71 $file = basename($name);
72 $path = preg_replace("/".$file."/", "", $name);
77 function check_authkey ( $key )
79 // TODO: Make real, actual checks...
80 if ( $key ) return true;
84 function update_authkey ( $session, $authid )
86 $key = substr(new_key(), 0, 8);
90 function check_session ( $name )
92 session_name( $name );
94 if ( ! $_SESSION['authid'] )
96 return clear_credentials($name);
98 if ( ! $_COOKIE['client_key'] )
100 return clear_credentials($name);
103 $authid = $_SESSION['authid'];
104 $type = $_SESSION['type'];
105 $client_key = $_COOKIE['client_key'];
107 $level = get_authorization( $type, $authid );
108 if ( $level == false )
110 return clear_credentials($name);
113 $session_key = md5( $name . $authid );
114 if ( $client_key != $session_key )
116 return clear_credentials($name);
119 // TODO: Database checks?
121 // TODO: Refresh cookie
123 // If we got this far, things are looking good.
127 function set_credentials( $authid, $type )
129 $name = new_key(true);
130 session_name( $name );
132 $_SESSION['authid'] = $authid;
133 $_SESSION['type'] = $type;
135 $client_key = md5( $name . $authid );
136 setcookie('client_key', $client_key, time()+
180*60, get_cookie_path() );
138 // TODO: Stuff data to database for further checks?
139 // TODO: Do magic with the KEY
144 function clear_credentials($name)
146 setcookie('client_key', '', 0, get_cookie_path() );
147 setcookie($name, '', 0, "/");
153 function get_authorization()
157 function can_write ( )
159 // Stub, to be called on any API nodes that write data in the DB.
160 $authid = $_SESSION['authid'];
161 $type = $_SESSION['type'];
162 $level = get_authorization( $type, $authid );