Skip to content

Commit 2860bda

Browse files
Added support for OAuth2 token authentication.
1 parent aace55a commit 2860bda

File tree

9 files changed

+65
-29
lines changed

9 files changed

+65
-29
lines changed

doc/src/conf.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,10 @@
4343
# |release|, also used in various other places throughout the built documents
4444
#
4545
# the short X.Y version
46-
version = '4.4'
46+
version = '4.5'
4747

4848
# the full version, including alpha/beta/rc tags
49-
release = '4.4.1'
49+
release = '4.5.0b1'
5050

5151
# the theme to use for HTML pages
5252
html_theme = 'oracle'

doc/src/releasenotes.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
ODPI-C Release notes
22
====================
33

4+
Version 4.5 (TBD)
5+
-----------------
6+
7+
#) Added support for OAuth token authentication when creating standalone
8+
connections and connection pools.
9+
10+
411
Version 4.4.1 (June 14, 2022)
512
-----------------------------
613

doc/src/structs/dpiAccessToken.rst

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,11 @@ The externalAuth must be set to 1 and in addition, homogeneous must be
1111
set to 1 when creating a pool. The user (or username) and password properties
1212
should not be set.
1313

14-
Both token and privateKey (and their lengths) must be set.
14+
Both token and privateKey (and their lengths) must be set while using IAM
15+
for token based authentication.
16+
17+
Only token and it's length must be set while using OAuth for token based
18+
authentication.
1519

1620
.. member:: const char* dpiAccessToken.token
1721

include/dpi.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,9 @@ extern "C" {
6969

7070
// define ODPI-C version information
7171
#define DPI_MAJOR_VERSION 4
72-
#define DPI_MINOR_VERSION 4
73-
#define DPI_PATCH_LEVEL 1
74-
#define DPI_VERSION_SUFFIX
72+
#define DPI_MINOR_VERSION 5
73+
#define DPI_PATCH_LEVEL 0
74+
#define DPI_VERSION_SUFFIX "b1"
7575

7676
#define DPI_STR_HELPER(x) #x
7777
#define DPI_STR(x) DPI_STR_HELPER(x)

samples/SampleLib.c

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -229,8 +229,14 @@ void dpiSamples__getTokenData(const char *dirName, const char *fileName,
229229
sprintf(fullFileName, "%s/%s", dirName, fileName);
230230

231231
fp = fopen(fullFileName, "r");
232-
if (!fp)
233-
dpiSamples__fatalError("Not able to open file.");
232+
free(fullFileName);
233+
fullFileName = NULL;
234+
235+
if (!fp) {
236+
*value = NULL;
237+
*valueLength = 0;
238+
return;
239+
}
234240

235241
*value = malloc(TOKENBUFLEN);
236242
if (!*value)
@@ -251,8 +257,6 @@ void dpiSamples__getTokenData(const char *dirName, const char *fileName,
251257
}
252258

253259
fclose(fp);
254-
free(fullFileName);
255-
fullFileName = NULL;
256260
}
257261

258262

src/dpiErrorMessages.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ static const char* const dpiErrorMessages[DPI_ERR_MAX - DPI_ERR_NO_ERR] = {
107107
"DPI-1078: native type %d with Oracle type %d is not supported by JSON", // DPI_ERR_UNHANDLED_CONVERSION_TO_JSON
108108
"DPI-1079: Oracle Client library is at version %d.%d but either version %d.%d (or later DBRU) or version %d.%d (or higher) is needed", // DPI_ERR_ORACLE_CLIENT_TOO_OLD_MULTI
109109
"DPI-1080: connection was closed by ORA-%d", // DPI_ERR_CONN_CLOSED
110-
"DPI-1081: invalid dpiAccessToken structure. Both the token and the private key must contain values", // DPI_ERR_TOKEN_BASED_AUTH
110+
"DPI-1081: invalid dpiAccessToken structure. The token must contain a value", // DPI_ERR_TOKEN_BASED_AUTH
111111
"DPI-1082: invalid connection pool configuration for token based authentication. Both homogeneous and externalAuth fields in the dpiPoolCreateParams structure must be set to 1", //DPI_ERR_POOL_TOKEN_BASED_AUTH
112112
"DPI-1083: invalid standalone connection configuration for token based authentication. The externalAuth field in the dpiConnCreateParams structure must be set to 1", //DPI_ERR_STANDALONE_TOKEN_BASED_AUTH
113113
};

src/dpiImpl.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -343,10 +343,11 @@ extern unsigned long dpiDebugLevel;
343343
#define DPI_OCI_ATTR_JSON_DOM_MUTABLE 609
344344
#define DPI_OCI_ATTR_SODA_METADATA_CACHE 624
345345
#define DPI_OCI_ATTR_SODA_HINT 627
346-
#define DPI_OCI_ATTR_IAM_TOKEN 636
346+
#define DPI_OCI_ATTR_TOKEN 636
347347
#define DPI_OCI_ATTR_IAM_PRIVKEY 637
348-
#define DPI_OCI_ATTR_IAM_CBK 638
349-
#define DPI_OCI_ATTR_IAM_CBKCTX 639
348+
#define DPI_OCI_ATTR_TOKEN_CBK 638
349+
#define DPI_OCI_ATTR_TOKEN_CBKCTX 639
350+
#define DPI_OCI_ATTR_TOKEN_ISBEARER 657
350351

351352
// define OCI object type constants
352353
#define DPI_OCI_OTYPE_NAME 1

src/dpiPool.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -156,16 +156,16 @@ static int dpiPool__create(dpiPool *pool, const char *userName,
156156
return DPI_FAILURE;
157157

158158
if (createParams->accessTokenCallback) {
159-
// set IAM context callback on session handle
159+
// set token based auth context callback on session handle
160160
if (dpiOci__attrSet(authInfo, DPI_OCI_HTYPE_SESSION,
161-
(void*) pool, 0, DPI_OCI_ATTR_IAM_CBKCTX,
161+
(void*) pool, 0, DPI_OCI_ATTR_TOKEN_CBKCTX,
162162
"set token callback context", error) < 0)
163163
return DPI_FAILURE;
164164

165-
// set IAM callback on session handle
165+
// set token based auth callback on session handle
166166
if (dpiOci__attrSet(authInfo, DPI_OCI_HTYPE_SESSION,
167167
(void*) dpiPool__accessTokenCallback, 0,
168-
DPI_OCI_ATTR_IAM_CBK, "set token callback", error) < 0)
168+
DPI_OCI_ATTR_TOKEN_CBK, "set token callback", error) < 0)
169169
return DPI_FAILURE;
170170
}
171171
}

src/dpiUtils.c

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -533,29 +533,49 @@ int dpiUtils__setAccessTokenAttributes(void *handle,
533533
dpiAccessToken *accessToken, dpiVersionInfo *versionInfo,
534534
dpiError *error)
535535
{
536-
// only available in Oracle Client 19.14+ and 21.5+ libraries
537-
if (dpiUtils__checkClientVersionMulti(versionInfo,
538-
19, 14, 21, 5, error) < 0)
539-
return DPI_FAILURE;
536+
int isBearer = 1;
540537

541538
// check validity of access token
542539
if (!accessToken->token || accessToken->tokenLength == 0 ||
543-
!accessToken->privateKey || accessToken->privateKeyLength == 0)
540+
(accessToken->privateKey && accessToken->privateKeyLength == 0))
544541
return dpiError__set(error,
545542
"check token based authentication parameters",
546543
DPI_ERR_TOKEN_BASED_AUTH);
547544

545+
// IAM feature only available in Oracle Client 19.14+ and 21.5+ libraries
546+
if (accessToken->privateKey) {
547+
if (dpiUtils__checkClientVersionMulti(versionInfo, 19, 14, 21, 5,
548+
error) < 0)
549+
return DPI_FAILURE;
550+
551+
// OAuth feature only available in Oracle Client 19.15+ and 21.7+ libraries
552+
} else {
553+
if (dpiUtils__checkClientVersionMulti(versionInfo, 19, 15, 21, 7,
554+
error) < 0)
555+
return DPI_FAILURE;
556+
}
557+
548558
// set token on auth handle
549559
if (dpiOci__attrSet(handle, DPI_OCI_HTYPE_AUTHINFO,
550560
(void*) accessToken->token, accessToken->tokenLength,
551-
DPI_OCI_ATTR_IAM_TOKEN, "set DB token", error) < 0)
561+
DPI_OCI_ATTR_TOKEN, "set access token", error) < 0)
552562
return DPI_FAILURE;
553563

554-
// set private key on auth handle
555-
if (dpiOci__attrSet(handle, DPI_OCI_HTYPE_AUTHINFO,
556-
(void*) accessToken->privateKey, accessToken->privateKeyLength,
557-
DPI_OCI_ATTR_IAM_PRIVKEY, "set DB token private key", error) < 0)
558-
return DPI_FAILURE;
564+
// set IAM private key on auth handle
565+
if (accessToken->privateKey) {
566+
if (dpiOci__attrSet(handle, DPI_OCI_HTYPE_AUTHINFO,
567+
(void*) accessToken->privateKey,
568+
accessToken->privateKeyLength, DPI_OCI_ATTR_IAM_PRIVKEY,
569+
"set access token private key", error) < 0)
570+
return DPI_FAILURE;
571+
572+
// set OAuth bearer flag on auth handle
573+
} else {
574+
if (dpiOci__attrSet(handle, DPI_OCI_HTYPE_AUTHINFO,
575+
(void*) &isBearer, 0, DPI_OCI_ATTR_TOKEN_ISBEARER,
576+
"set bearer flag", error) < 0)
577+
return DPI_FAILURE;
578+
}
559579

560580
return DPI_SUCCESS;
561581
}

0 commit comments

Comments
 (0)