aboutsummaryrefslogtreecommitdiffstats
path: root/lib/oauth2.h
blob: c8d18963bfb3598527bd637bcdc8b8a05ad4caa3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
/***************************************************************************\
*                                                                           *
*  BitlBee - An IRC to IM gateway                                           *
*  Simple OAuth2 client (consumer) implementation.                          *
*                                                                           *
*  Copyright 2010-2011 Wilmer van der Gaast <wilmer@gaast.net>              *
*                                                                           *
*  This program is free software; you can redistribute it and/or modify     *
*  it under the terms of the GNU General Public License as published by     *
*  the Free Software Foundation; either version 2 of the License, or        *
*  (at your option) any later version.                                      *
*                                                                           *
*  This program is distributed in the hope that it will be useful,          *
*  but WITHOUT ANY WARRANTY; without even the implied warranty of           *
*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            *
*  GNU General Public License for more details.                             *
*                                                                           *
*  You should have received a copy of the GNU General Public License along  *
*  with this program; if not, write to the Free Software Foundation, Inc.,  *
*  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.              *
*                                                                           *
\***************************************************************************/

/* Implementation mostly based on my experience with writing the previous OAuth
   module, and from http://code.google.com/apis/accounts/docs/OAuth2.html . */

typedef void (*oauth2_token_callback)( gpointer data, const char *atoken, const char *rtoken );

struct oauth2_service
{
	char *auth_url;
	char *token_url;
	char *redirect_url;
	char *scope;
	char *consumer_key;
	char *consumer_secret;
};

#define OAUTH2_AUTH_CODE "authorization_code"
#define OAUTH2_AUTH_REFRESH "refresh_token"

/* Generate a URL the user should open in his/her browser to get an
   authorization code. */
char *oauth2_url( const struct oauth2_service *sp );

/* Exchanges an auth code or refresh token for an access token.
   auth_type is one of the two OAUTH2_AUTH_.. constants above. */
int oauth2_access_token( const struct oauth2_service *sp,
                         const char *auth_type, const char *auth,
                         oauth2_token_callback func, gpointer data );
General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License with the Debian GNU/Linux distribution in /usr/share/common-licenses/GPL; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #include <openssl/crypto.h> #include <openssl/rand.h> #include <openssl/x509.h> #include <openssl/pem.h> #include <openssl/ssl.h> #include <openssl/err.h> #include "proxy.h" #include "ssl_client.h" #include "sock.h" int ssl_errno = 0; static gboolean initialized = FALSE; struct scd { ssl_input_function func; gpointer data; int fd; gboolean established; int inpa; int lasterr; /* Necessary for SSL_get_error */ SSL *ssl; SSL_CTX *ssl_ctx; }; static void ssl_connected( gpointer data, gint source, GaimInputCondition cond ); void *ssl_connect( char *host, int port, ssl_input_function func, gpointer data ) { struct scd *conn = g_new0( struct scd, 1 ); SSL_METHOD *meth; conn->fd = proxy_connect( host, port, ssl_connected, conn ); conn->func = func; conn->data = data; if( conn->fd < 0 ) { g_free( conn ); return( NULL ); } if( !initialized ) { initialized = TRUE; SSLeay_add_ssl_algorithms(); } meth = TLSv1_client_method(); conn->ssl_ctx = SSL_CTX_new( meth ); if( conn->ssl_ctx == NULL ) { conn->fd = -1; return( NULL ); } conn->ssl = SSL_new( conn->ssl_ctx ); if( conn->ssl == NULL ) { conn->fd = -1; return( NULL ); } return( conn ); } static void ssl_handshake( gpointer data, gint source, GaimInputCondition cond ); static void ssl_connected( gpointer data, gint source, GaimInputCondition cond ) { struct scd *conn = data; if( source == -1 ) return ssl_handshake( data, -1, cond ); /* Make it non-blocking at least during the handshake... */ sock_make_nonblocking( conn->fd ); SSL_set_fd( conn->ssl, conn->fd ); return ssl_handshake( data, source, cond ); } static void ssl_handshake( gpointer data, gint source, GaimInputCondition cond ) { struct scd *conn = data; int st; if( conn->inpa != -1 ) { gaim_input_remove( conn->inpa ); conn->inpa = -1; } if( ( st = SSL_connect( conn->ssl ) ) < 0 ) { conn->lasterr = SSL_get_error( conn->ssl, st ); if( conn->lasterr != SSL_ERROR_WANT_READ && conn->lasterr != SSL_ERROR_WANT_WRITE ) goto ssl_connected_failure; conn->inpa = gaim_input_add( conn->fd, ssl_getdirection( conn ), ssl_handshake, data ); return; } conn->established = TRUE; sock_make_blocking( conn->fd ); /* For now... */ conn->func( conn->data, conn, cond ); return; ssl_connected_failure: conn->func( conn->data, NULL, cond ); if( conn->ssl ) { SSL_shutdown( conn->ssl ); SSL_free( conn->ssl ); } if( conn->ssl_ctx ) { SSL_CTX_free( conn->ssl_ctx ); } if( source >= 0 ) closesocket( source ); g_free( conn ); } int ssl_read( void *conn, char *buf, int len ) { int st; if( !((struct scd*)conn)->established ) { ssl_errno = SSL_NOHANDSHAKE; return -1; } st = SSL_read( ((struct scd*)conn)->ssl, buf, len ); ssl_errno = SSL_OK; if( st <= 0 ) { ((struct scd*)conn)->lasterr = SSL_get_error( ((struct scd*)conn)->ssl, st ); if( ((struct scd*)conn)->lasterr == SSL_ERROR_WANT_READ || ((struct scd*)conn)->lasterr == SSL_ERROR_WANT_WRITE ) ssl_errno = SSL_AGAIN; } return st; } int ssl_write( void *conn, const char *buf, int len ) { int st; if( !((struct scd*)conn)->established ) { ssl_errno = SSL_NOHANDSHAKE; return -1; } st = SSL_write( ((struct scd*)conn)->ssl, buf, len ); ssl_errno = SSL_OK; if( st <= 0 ) { ((struct scd*)conn)->lasterr = SSL_get_error( ((struct scd*)conn)->ssl, st ); if( ((struct scd*)conn)->lasterr == SSL_ERROR_WANT_READ || ((struct scd*)conn)->lasterr == SSL_ERROR_WANT_WRITE ) ssl_errno = SSL_AGAIN; } return st; } void ssl_disconnect( void *conn_ ) { struct scd *conn = conn_; if( conn->inpa != -1 ) gaim_input_remove( conn->inpa ); if( conn->established ) SSL_shutdown( conn->ssl ); closesocket( conn->fd ); SSL_free( conn->ssl ); SSL_CTX_free( conn->ssl_ctx ); g_free( conn ); } int ssl_getfd( void *conn ) { return( ((struct scd*)conn)->fd ); } GaimInputCondition ssl_getdirection( void *conn ) { return( ((struct scd*)conn)->lasterr == SSL_ERROR_WANT_WRITE ? GAIM_INPUT_WRITE : GAIM_INPUT_READ ); }