]> git.defcon.no Git - hermes/blob - api/lib/auth_base.php
First stab at auth. Flow-changes to make things sort'a work
[hermes] / api / lib / auth_base.php
1 <?php
2 require_once('config.php');
3
4 $config = get_config();
5
6 /*******************************
7 * Load authentication plugin ..
8 *******************************/
9 if ( preg_match('/^\w+$/', $config['auth_backend']))
10 {
11 if ( !@include_once ( 'lib/auth_plugins/' . $config['auth_backend'] . ".php" ) )
12 { print json_encode( array( 'response' => 'error', 'cause' => 'auth-load' ) ); exit; }
13 }
14 else
15 { print json_encode( array( 'response' => 'error', 'cause' => 'config-error' ) ); exit; }
16 /*******************************/
17
18 function check_authkey ( $key )
19 {
20 global $config;
21 if ( $key == "6327c08b70f9" ) return true;
22 return false;
23
24 }
25
26 function new_key( )
27 {
28 // Basically this is at the moment a slightly modified
29 // version of generate_password() from user_functiions.php
30 // The behaviour/output of this function is expected to change
31 // so using generate_password() directly does not make sense...
32 $length = 16;
33 $string = "";
34 while ( strlen( $string ) < $length )
35 {
36 $string .= crypt( substr(sha1(rand()), 0, $length) );
37 $string = preg_replace( '/\W/', '', $string);
38 }
39 return substr( $string, 0, $length );
40 }
41
42 function simple_authfail()
43 {
44 print json_encode( array( 'response' => 'failed', 'cause' => 'unauthorized', 'description' => 'Not authorized') );
45 exit;
46 }
47
48 function token_auth( )
49 {
50 global $_GET;
51
52 if ( array_key_exists('auth_key', $_GET ) )
53 { if ( ! check_authkey($_GET['auth_key'] ) ) simple_authfail(); }
54 else simple_authfail();
55 }
56
57 function can_write ( )
58 {
59 // Stub, to be called on any API nodes taht write data in the DB.
60 return true;
61 }
62
63 ?>