mirror of https://github.com/icedream/npsharp.git
				
				
				
			NPv2 auth support bits
							parent
							
								
									7f9cc54c54
								
							
						
					
					
						commit
						58a5e8049d
					
				|  | @ -187,9 +187,9 @@ namespace NPSharp.NP | |||
|         ///     Authenticates a server ticket. | ||||
|         /// </summary> | ||||
|         /// <returns>True if the ticket validation succeeded, otherwise false.</returns> | ||||
|         public async Task<bool> ValidateTicket(IPAddress clientIP, ulong guid, Ticket ticket) | ||||
|         public async Task<TicketValidationResult> ValidateTicket(IPAddress clientIP, ulong guid, Ticket ticket) | ||||
|         { | ||||
|             var tcs = new TaskCompletionSource<bool>(); | ||||
|             var tcs = new TaskCompletionSource<TicketValidationResult>(); | ||||
| 
 | ||||
|             RPC.AttachHandlerForNextMessage(packet => | ||||
|             { | ||||
|  | @ -197,7 +197,7 @@ namespace NPSharp.NP | |||
|                 if (result == null) | ||||
|                     return; | ||||
| 
 | ||||
|                 tcs.SetResult(result.Result == 0); | ||||
|                 tcs.SetResult(new TicketValidationResult(result)); | ||||
|             }); | ||||
| 
 | ||||
|             RPC.Send(new AuthenticateValidateTicketMessage | ||||
|  |  | |||
|  | @ -35,6 +35,9 @@ | |||
|     <Reference Include="log4net"> | ||||
|       <HintPath>$(SolutionDir)\packages\log4net.2.0.3\lib\net40-full\log4net.dll</HintPath> | ||||
|     </Reference> | ||||
|     <Reference Include="Newtonsoft.Json"> | ||||
|       <HintPath>..\..\..\..\packages\Newtonsoft.Json.7.0.1-beta1\lib\net45\Newtonsoft.Json.dll</HintPath> | ||||
|     </Reference> | ||||
|     <Reference Include="protobuf-net"> | ||||
|       <HintPath>$(SolutionDir)\packages\protobuf-net.2.0.0.668\lib\net40\protobuf-net.dll</HintPath> | ||||
|     </Reference> | ||||
|  |  | |||
|  | @ -1,11 +1,65 @@ | |||
| using System.Collections.Generic; | ||||
| using System.IO; | ||||
| 
 | ||||
| using Newtonsoft.Json.Linq; | ||||
| 
 | ||||
| namespace NPSharp.RPC.Messages.Data | ||||
| { | ||||
|     /// <summary> | ||||
|     ///     Represents the outcome of a ticket validation attempt. | ||||
|     ///     Represents the outcome of a ticket validation attempt, including eventual NPv2 authentication identifiers passed by the server. | ||||
|     /// </summary> | ||||
|     public enum TicketValidationResult | ||||
|     public class TicketValidationResult | ||||
|     { | ||||
|         Valid = 0, | ||||
|         Invalid = 1 | ||||
|         internal TicketValidationResult(Server.AuthenticateValidateTicketResultMessage message) | ||||
|         { | ||||
|             IsValid = message.Result == 0; | ||||
| 
 | ||||
|             Identifiers = ParseIdentifierList(message.Identifiers); | ||||
|         } | ||||
| 
 | ||||
|         internal IEnumerable<string> ParseIdentifierList(string serializedList) | ||||
|         { | ||||
|             // current layer1 implementation uses JSON, formatted as `[ [ <type>, <value> ]... ]` - we'll concatenate these as strings to prevent this implementation detail | ||||
|             // this is consistent with the external API for `profiles` in Citizen itself | ||||
|             JToken jsonValue; | ||||
| 
 | ||||
|             try | ||||
|             { | ||||
|                 jsonValue = JToken.Parse(serializedList); | ||||
|             } | ||||
|             catch (FileLoadException) | ||||
|             { | ||||
|                 return new string[0]; | ||||
|             } | ||||
| 
 | ||||
|             var identifiers = new List<string>(); | ||||
| 
 | ||||
|             if (jsonValue.Type == JTokenType.Array) | ||||
|             { | ||||
|                 var array = (JArray)jsonValue; | ||||
| 
 | ||||
|                 foreach (var identifierToken in array.Children()) | ||||
|                 { | ||||
|                     if (identifierToken.Type == JTokenType.Array) | ||||
|                     { | ||||
|                         var identifierArray = (JArray)identifierToken; | ||||
| 
 | ||||
|                         identifiers.Add(string.Format("{0}:{1}", identifierArray[0], identifierArray[1])); | ||||
|                     } | ||||
|                 } | ||||
|             } | ||||
| 
 | ||||
|             return identifiers; | ||||
|         } | ||||
| 
 | ||||
|         /// <summary> | ||||
|         /// Whether the ticket is valid or not. | ||||
|         /// </summary> | ||||
|         public bool IsValid { get; private set; } | ||||
| 
 | ||||
|         /// <summary> | ||||
|         /// A list of NPv2 authentication identifiers belonging to the ticket session. | ||||
|         /// </summary> | ||||
|         public IEnumerable<string> Identifiers { get; private set; } | ||||
|     } | ||||
| } | ||||
|  | @ -15,5 +15,8 @@ namespace NPSharp.RPC.Messages.Server | |||
| 
 | ||||
|         [ProtoMember(3)] | ||||
|         public int GroupID { get; set; } | ||||
| 
 | ||||
|         [ProtoMember(4, IsRequired = false)] | ||||
|         public string Identifiers { get; set; } | ||||
|     } | ||||
| } | ||||
|  | @ -2,5 +2,6 @@ | |||
| 
 | ||||
| <packages> | ||||
|   <package id="log4net" version="2.0.3" targetFramework="net45" /> | ||||
|   <package id="Newtonsoft.Json" version="7.0.1-beta1" targetFramework="net45" /> | ||||
|   <package id="protobuf-net" version="2.0.0.668" targetFramework="net45" /> | ||||
| </packages> | ||||
		Loading…
	
		Reference in New Issue