<?xml version="1.0" encoding="utf-8"?>
<xsd:schema 
	xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
	xmlns:tns="http://ksef.mf.gov.pl/auth/token/2.0" 
	targetNamespace="http://ksef.mf.gov.pl/auth/token/2.0" 
	elementFormDefault="qualified" attributeFormDefault="unqualified">

	<xsd:element name="AuthTokenRequest">
		<xsd:complexType>
			<xsd:sequence>
				<xsd:element name="Challenge">
					<xsd:simpleType>
						<xsd:restriction base="xsd:token">
							<xsd:length value="36"/>
							<xsd:pattern value="[0-9]{8}-CR-[A-F0-9]{10}-[A-F0-9]{10}-[A-F0-9]{2}"/>
						</xsd:restriction>
					</xsd:simpleType>
				</xsd:element>

				<xsd:element name="ContextIdentifier" type="tns:TContextIdentifier"/>
				<xsd:element name="SubjectIdentifierType" type="tns:SubjectIdentifierTypeEnum"/>

				<xsd:element name="AuthorizationPolicy" minOccurs="0">
					<xsd:complexType>
						<xsd:sequence>
							<xsd:element name="AllowedIps">
								<xsd:complexType>
									<xsd:sequence minOccurs="1" maxOccurs="1">
										<xsd:element name="Ip4Address" minOccurs="0" maxOccurs="10">
											<xsd:simpleType>
												<xsd:restriction base="xsd:token">
													<xsd:pattern value="((25[0-5]|(2[0-4]|1[0-9]|[0-9]{1,2}))\.){3}(25[0-5]|(2[0-4]|1[0-9]|[0-9]{1,2}))"/>
												</xsd:restriction>
											</xsd:simpleType>
										</xsd:element>

										<xsd:element name="Ip4Range" minOccurs="0" maxOccurs="10">
											<xsd:simpleType>
												<xsd:restriction base="xsd:token">
													<xsd:pattern value="((25[0-5]|(2[0-4]|1[0-9]|[0-9]{1,2}))\.){3}(25[0-5]|(2[0-4]|1[0-9]|[0-9]{1,2}))-((25[0-5]|(2[0-4]|1[0-9]|[0-9]{1,2}))\.){3}(25[0-5]|(2[0-4]|1[0-9]|[0-9]{1,2}))"/>
												</xsd:restriction>
											</xsd:simpleType>
										</xsd:element>

										<xsd:element name="Ip4Mask" minOccurs="0" maxOccurs="10">
											<xsd:simpleType>
												<xsd:restriction base="xsd:token">
													<xsd:pattern value="((25[0-5]|(2[0-4]|1[0-9]|[0-9]{1,2}))\.){3}(25[0-5]|(2[0-4]|1[0-9]|[0-9]{1,2}))/([0-9]|[1-2][0-9]|3[0-2])"/>
												</xsd:restriction>
											</xsd:simpleType>
										</xsd:element>

									</xsd:sequence>
								</xsd:complexType>
							</xsd:element>
						</xsd:sequence>
					</xsd:complexType>
				</xsd:element>

			</xsd:sequence>
		</xsd:complexType>
	</xsd:element>

	<xsd:simpleType name="SubjectIdentifierTypeEnum">
		<xsd:restriction base="xsd:token">
			<xsd:enumeration value="certificateSubject"/>
			<xsd:enumeration value="certificateFingerprint"/>
		</xsd:restriction>
	</xsd:simpleType>	
	
	<xsd:complexType name="TContextIdentifier">
		<xsd:choice>
			<xsd:element name="Nip" type="tns:TNIP"/>
			<xsd:element name="InternalId" type="tns:TIID"/>
			<xsd:element name="NipVatUe" type="tns:TNipVatUE"/>
			<xsd:element name="PeppolId" type="tns:TPeppolId"/>
		</xsd:choice>
	</xsd:complexType>
	
	<xsd:simpleType name="TIID">
		<xsd:restriction base="xsd:string">
			<xsd:pattern value="[1-9](([0-9][1-9])|([1-9][0-9]))[0-9]{7}-[0-9]{5}"/>
		</xsd:restriction>
	</xsd:simpleType>

	<xsd:simpleType name="TNIP">
		<xsd:restriction base="xsd:string">
			<xsd:pattern value="[1-9](([0-9][1-9])|([1-9][0-9]))[0-9]{7}"/>
		</xsd:restriction>
	</xsd:simpleType>

	<xsd:simpleType name="TNipVatUE">
		<xsd:restriction base="xsd:string">
			<xsd:pattern value="([1-9](([0-9][1-9])|([1-9][0-9]))[0-9]{7}-((AT)(U[0-9]{8})|(BE)([01]{1}[0-9]{9})|(BG)([0-9]{9,10})|(CY)([0-9]{8}[A-Z])|(CZ)([0-9]{8,10})|(DE)([0-9]{9})|(DK)([0-9]{8})|(EE)([0-9]{9})|(EL)([0-9]{9})|(ES)([A-Z][0-9]{8}|[0-9]{8}[A-Z]|[A-Z][0-9]{7}[A-Z])|(FI)([0-9]{8})|(FR)[A-Z0-9]{2}[0-9]{9}|(HR)([0-9]{11})|(HU)([0-9]{8})|(IE)([0-9]{7}[A-Z]{2}|[0-9][A-Z0-9+*][0-9]{5}[A-Z])|(IT)([0-9]{11})|(LT)([0-9]{9}|[0-9]{12})|(LU)([0-9]{8})|(LV)([0-9]{11})|(MT)([0-9]{8})|(NL)([A-Z0-9+*]{12})|(PT)([0-9]{9})|(RO)([0-9]{2,10})|(SE)([0-9]{12})|(SI)([0-9]{8})|(SK)([0-9]{10})|(XI)(([0-9]{9}|([0-9]{12}))|(GD|HA)([0-9]{3}))))$"/>
		</xsd:restriction>
	</xsd:simpleType>

	<xsd:simpleType name="TPeppolId">
		<xsd:restriction base="xsd:string">
			<xsd:pattern value="P[A-Z]{2}[0-9]{6}"/>
		</xsd:restriction>
	</xsd:simpleType>

</xsd:schema>
