@@ -48,16 +48,14 @@ public async Task<bool> AuthenticateRequest(HttpRequestMessage request)
48
48
if ( options . DisableV1 && version == MAuthVersion . MWS )
49
49
throw new InvalidVersionException ( $ "Authentication with { version } version is disabled.") ;
50
50
51
- var mAuthCore = MAuthCoreFactory . Instantiate ( version ) ;
52
- var authInfo = GetAuthenticationInfo ( request , version ) ;
53
- var logMessage = "Mauth-client attempting to authenticate request from app with mauth app uuid" +
54
- $ " { authInfo . ApplicationUuid } using version { version } ";
55
- logger . LogInformation ( logMessage ) ;
56
-
57
- var appInfo = await GetApplicationInfo ( authInfo . ApplicationUuid , version ) . ConfigureAwait ( false ) ;
58
- var signature = await mAuthCore . GetSignature ( request , authInfo ) . ConfigureAwait ( false ) ;
59
-
60
- return mAuthCore . Verify ( authInfo . Payload , signature , appInfo . PublicKey ) ;
51
+ var authenticated = await Authenticate ( request , version ) . ConfigureAwait ( false ) ;
52
+ if ( ! authenticated && version == MAuthVersion . MWSV2 && ! options . DisableV1 )
53
+ {
54
+ // fall back to V1 authentication
55
+ authenticated = await Authenticate ( request , MAuthVersion . MWS ) . ConfigureAwait ( false ) ;
56
+ logger . LogWarning ( "Completed successful authentication attempt after fallback to V1" ) ;
57
+ }
58
+ return authenticated ;
61
59
}
62
60
catch ( ArgumentException ex )
63
61
{
@@ -91,10 +89,24 @@ public async Task<bool> AuthenticateRequest(HttpRequestMessage request)
91
89
}
92
90
}
93
91
94
- private Task < ApplicationInfo > GetApplicationInfo ( Guid applicationUuid , MAuthVersion version ) =>
92
+ private async Task < bool > Authenticate ( HttpRequestMessage request , MAuthVersion version )
93
+ {
94
+ var logMessage = "Mauth-client attempting to authenticate request from app with mauth app uuid" +
95
+ $ " { options . ApplicationUuid } using version { version } ";
96
+ logger . LogInformation ( logMessage ) ;
97
+
98
+ var mAuthCore = MAuthCoreFactory . Instantiate ( version ) ;
99
+ var authInfo = GetAuthenticationInfo ( request , mAuthCore ) ;
100
+ var appInfo = await GetApplicationInfo ( authInfo . ApplicationUuid ) . ConfigureAwait ( false ) ;
101
+
102
+ var signature = await mAuthCore . GetSignature ( request , authInfo ) . ConfigureAwait ( false ) ;
103
+ return mAuthCore . Verify ( authInfo . Payload , signature , appInfo . PublicKey ) ;
104
+ }
105
+
106
+ private Task < ApplicationInfo > GetApplicationInfo ( Guid applicationUuid ) =>
95
107
cache . GetOrCreateAsync ( applicationUuid , async entry =>
96
108
{
97
- var retrier = new MAuthRequestRetrier ( options , version ) ;
109
+ var retrier = new MAuthRequestRetrier ( options ) ;
98
110
var response = await retrier . GetSuccessfulResponse (
99
111
applicationUuid ,
100
112
CreateRequest ,
@@ -111,38 +123,41 @@ private Task<ApplicationInfo> GetApplicationInfo(Guid applicationUuid, MAuthVers
111
123
return result ;
112
124
} ) ;
113
125
114
- private HttpRequestMessage CreateRequest ( Guid applicationUuid ) =>
115
- new HttpRequestMessage ( HttpMethod . Get , new Uri ( options . MAuthServiceUrl ,
116
- $ "{ Constants . MAuthTokenRequestPath } { applicationUuid . ToHyphenString ( ) } .json") ) ;
117
-
118
126
/// <summary>
119
127
/// Extracts the authentication information from a <see cref="HttpRequestMessage"/>.
120
128
/// </summary>
121
129
/// <param name="request">The request that has the authentication information.</param>
122
- /// <param name="version">Enum value of the MAuthVersion .</param>
130
+ /// <param name="mAuthCore">Instantiation of mAuthCore class .</param>
123
131
/// <returns>The authentication information with the payload from the request.</returns>
124
- internal PayloadAuthenticationInfo GetAuthenticationInfo ( HttpRequestMessage request , MAuthVersion version )
132
+ internal static PayloadAuthenticationInfo GetAuthenticationInfo ( HttpRequestMessage request , IMAuthCore mAuthCore )
125
133
{
126
- var mAuthCore = MAuthCoreFactory . Instantiate ( version ) ;
127
134
var headerKeys = mAuthCore . GetHeaderKeys ( ) ;
128
135
var authHeader = request . Headers . GetFirstValueOrDefault < string > ( headerKeys . mAuthHeaderKey ) ;
129
136
130
137
if ( authHeader == null )
138
+ {
131
139
throw new ArgumentNullException ( nameof ( authHeader ) , "The MAuth header is missing from the request." ) ;
140
+ }
132
141
133
142
var signedTime = request . Headers . GetFirstValueOrDefault < long > ( headerKeys . mAuthTimeHeaderKey ) ;
134
143
135
144
if ( signedTime == default ( long ) )
145
+ {
136
146
throw new ArgumentException ( "Invalid MAuth signed time header value." , nameof ( signedTime ) ) ;
147
+ }
137
148
138
149
var ( uuid , payload ) = authHeader . ParseAuthenticationHeader ( ) ;
139
150
140
- return new PayloadAuthenticationInfo ( )
151
+ return new PayloadAuthenticationInfo
141
152
{
142
153
ApplicationUuid = uuid ,
143
154
Payload = Convert . FromBase64String ( payload ) ,
144
155
SignedTime = signedTime . FromUnixTimeSeconds ( )
145
156
} ;
146
157
}
158
+
159
+ private HttpRequestMessage CreateRequest ( Guid applicationUuid ) =>
160
+ new HttpRequestMessage ( HttpMethod . Get , new Uri ( options . MAuthServiceUrl ,
161
+ $ "{ Constants . MAuthTokenRequestPath } { applicationUuid . ToHyphenString ( ) } .json") ) ;
147
162
}
148
163
}
0 commit comments