File: DemoTokenPoolWithCallback.c

package info (click to toggle)
odpic 4.6.0-1
  • links: PTS, VCS
  • area: contrib
  • in suites: bookworm, sid
  • size: 4,052 kB
  • sloc: ansic: 40,206; sql: 2,128; makefile: 213; python: 69; sh: 15
file content (129 lines) | stat: -rw-r--r-- 5,334 bytes parent folder | download
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
//-----------------------------------------------------------------------------
// Copyright (c) 2016, 2022, Oracle and/or its affiliates.
//
// This software is dual-licensed to you under the Universal Permissive License
// (UPL) 1.0 as shown at https://oss.oracle.com/licenses/upl and Apache License
// 2.0 as shown at http://www.apache.org/licenses/LICENSE-2.0. You may choose
// either license.
//
// If you elect to accept the software under the Apache License, Version 2.0,
// the following applies:
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//    https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//-----------------------------------------------------------------------------

//-----------------------------------------------------------------------------
// NAME
//   DemoTokenPoolWithCallback.c
//
// DESCRIPTION
//   This demo file shows connection pooling with token based authentication to
//   Oracle Autonomous Database from Oracle Compute Infrastructure. It shows
//   how to create a connection pool and update expired tokens.
//
// PREREQUISITES
//   - Oracle Client libraries 19.14 (or later), or 21.5 (or later).
//
//   - The Oracle Cloud Infrastructure command line interface (OCI-CLI).  The
//     command line interface (CLI) is a tool that enables you to work with
//     Oracle Cloud Infrastructure objects and services at a command line, see
//     https://docs.oracle.com/en-us/iaas/Content/API/Concepts/cliconcepts.htm
//
//   - Set these environment variables (see the code explanation):
//      ODPIC_SAMPLES_ACCESS_TOKEN_LOC
//      ODPIC_SAMPLES_CONNECT_STRING
//      ODPIC_SAMPLES_EXPIRED_ACCESS_TOKEN_LOC
//-----------------------------------------------------------------------------

#include "SampleLib.h"

//-----------------------------------------------------------------------------
// tokenCallback()
//-----------------------------------------------------------------------------
int tokenCallback(void *context, dpiAccessToken *tokenRefresh)
{
    printf("Token callback is called.\n");

    // used for extracting the token and private key from files generated
    // through the OCI-CLI.
    dpiAccessToken *accessToken;
    accessToken = dpiSamples_getAccessToken();
    tokenRefresh->token = accessToken->token;
    tokenRefresh->tokenLength = accessToken->tokenLength;
    tokenRefresh->privateKey = accessToken->privateKey;
    tokenRefresh->privateKeyLength = accessToken->privateKeyLength;
    return DPI_SUCCESS;
}


//-----------------------------------------------------------------------------
// main()
//-----------------------------------------------------------------------------
int main(int argc, char **argv)
{
    dpiConn *conn1, *conn2;
    dpiPool *pool;
    dpiSampleParams *params;
    dpiAccessToken *accessToken;

    accessToken = dpiSamples_getAccessToken();
    params = dpiSamples_getParams();

    dpiPoolCreateParams poolCreateParams;
    if (dpiContext_initPoolCreateParams(params->context,
            &poolCreateParams) < 0)
        return dpiSamples_showError();

    // params for token based authentication:
    // accessTokenCallback: A callback function to provide a refreshed token
    // externalAuth:    Must be set to true for token based authentication
    // homogeneous:     Must be set to true for token based authentication
    poolCreateParams.minSessions = 1;
    poolCreateParams.maxSessions = 10;
    poolCreateParams.sessionIncrement = 1;
    poolCreateParams.externalAuth = 1;
    poolCreateParams.homogeneous = 1;
    poolCreateParams.accessTokenCallback = tokenCallback;

    dpiCommonCreateParams commonCreateParams;
    if (dpiContext_initCommonCreateParams(params->context,
            &commonCreateParams) < 0)
        return dpiSamples_showError();
    commonCreateParams.accessToken = accessToken;

    if (dpiPool_create(params->context, NULL, 0, NULL, 0,
            params->connectString, params->connectStringLength,
            &commonCreateParams, &poolCreateParams, &pool) < 0)
       return dpiSamples_showError();
    printf("Connection pool is created.\n");

    if (dpiPool_acquireConnection(pool, NULL, 0, NULL, 0, NULL,
            &conn1) < 0)
        return dpiSamples_showError();
    printf("Session is acquired from connection pool.\n");

    // set expired token and private key in connection pool
    dpiAccessToken expiredAccessToken;
    dpiSamples_populateAccessToken(&expiredAccessToken,
            "ODPIC_SAMPLES_EXPIRED_ACCESS_TOKEN_LOC");
    if (dpiPool_setAccessToken(pool, &expiredAccessToken) < 0)
        return dpiSamples_showError();

    // accessTokenCallback will get invoked to get refreshed tokens
    if (dpiPool_acquireConnection(pool, NULL, 0, NULL, 0, NULL,
            &conn2) < 0)
        return dpiSamples_showError();
    printf("Session is acquired from connection pool.\n");
    printf("Done.\n");
    return 0;
}