File: DbConnectionPoolAuthenticationContext.cs

package info (click to toggle)
mono 6.14.1%2Bds2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,282,732 kB
  • sloc: cs: 11,182,461; xml: 2,850,281; ansic: 699,123; cpp: 122,919; perl: 58,604; javascript: 30,841; asm: 21,845; makefile: 19,602; sh: 10,973; python: 4,772; pascal: 925; sql: 859; sed: 16; php: 1
file content (108 lines) | stat: -rw-r--r-- 4,579 bytes parent folder | download | duplicates (7)
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
//------------------------------------------------------------------------------
// <copyright file="DbConnectionPoolAuthenticationContext.cs" company="Microsoft">
//     Copyright (c) Microsoft Corporation.  All rights reserved.
// </copyright>
// <owner current="true" primary="true">harsudan</owner>
// <owner current="true" primary="false">yuronhe</owner>
//------------------------------------------------------------------------------

using System;
using System.Diagnostics;
using System.Runtime.ConstrainedExecution;
using System.Threading;

namespace System.Data.ProviderBase
{
    /// <summary>
    /// Represents the context of an authentication attempt when using the new active directory based authentication mechanisms.
    /// All data members, except_isUpdateInProgressCounter, should be immutable.
    /// </summary>
    sealed internal class DbConnectionPoolAuthenticationContext
    {
        /// <summary>
        /// The value expected in _isUpdateInProgress if a thread has taken a lock on this context,
        /// to perform the update on the context.
        /// </summary>
        private const int STATUS_LOCKED = 1;

        /// <summary>
        /// The value expected in _isUpdateInProgress if no thread has taken a lock on this context.
        /// </summary>
        private const int STATUS_UNLOCKED = 0;

        /// <summary>
        /// Access Token, which is obtained from Active Directory Authentication Library for SQL Server, and needs to be sent to SQL Server
        /// as part of TDS Token type Federated Authentication Token.
        /// </summary>
        private readonly byte[] _accessToken;

        /// <summary>
        /// Expiration time of the above access token.
        /// </summary>
        private readonly DateTime _expirationTime;

        /// <summary>
        /// A member which is used to achieve a lock to control refresh attempt on this context.
        /// </summary>
        private int _isUpdateInProgress;

        /// <summary>
        /// Constructor.
        /// </summary>
        /// <param name="accessToken">Access Token that will be used to connect to SQL Server. Carries identity information about a user.</param>
        /// <param name="expirationTime">The expiration time in UTC for the above accessToken.</param>
        internal DbConnectionPoolAuthenticationContext(byte[] accessToken, DateTime expirationTime) {

            Debug.Assert(accessToken != null && accessToken.Length > 0);
            Debug.Assert(expirationTime > DateTime.MinValue && expirationTime < DateTime.MaxValue);

            _accessToken = accessToken;
            _expirationTime = expirationTime;
            _isUpdateInProgress = STATUS_UNLOCKED;
        }

        /// <summary>
        /// Static Method.
        /// Given two contexts, choose one to update in the cache. Chooses based on expiration time.
        /// </summary>
        /// <param name="context1">Context1</param>
        /// <param name="context2">Context2</param>
        internal static DbConnectionPoolAuthenticationContext ChooseAuthenticationContextToUpdate(DbConnectionPoolAuthenticationContext context1, DbConnectionPoolAuthenticationContext context2) {

            Debug.Assert(context1 != null, "context1 should not be null.");
            Debug.Assert(context2 != null, "context2 should not be null.");

            return context1.ExpirationTime > context2.ExpirationTime ? context1 : context2;
        }

        internal byte[] AccessToken {
            get {
                return _accessToken;
            }
        }

        internal DateTime ExpirationTime {
            get {
                return _expirationTime;
            }
        }

        /// <summary>
        /// Try locking the variable _isUpdateInProgressCounter and return if this thread got the lock to update.
        /// Whichever thread got the chance to update this variable to 1 wins the lock.
        /// </summary>
        internal bool LockToUpdate() {
            int oldValue = Interlocked.CompareExchange(ref _isUpdateInProgress, STATUS_LOCKED, STATUS_UNLOCKED);
            return (oldValue == STATUS_UNLOCKED);
        }

        /// <summary>
        /// Release the lock which was obtained through LockToUpdate.
        /// </summary>
        [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
        internal void ReleaseLockToUpdate() {
            int oldValue = Interlocked.CompareExchange(ref _isUpdateInProgress, STATUS_UNLOCKED, STATUS_LOCKED);
            Debug.Assert(oldValue == STATUS_LOCKED);
        }
    }
}