001/* 002 * oauth2-oidc-sdk 003 * 004 * Copyright 2012-2016, Connect2id Ltd and contributors. 005 * 006 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use 007 * this file except in compliance with the License. You may obtain a copy of the 008 * License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, software distributed 013 * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 014 * CONDITIONS OF ANY KIND, either express or implied. See the License for the 015 * specific language governing permissions and limitations under the License. 016 */ 017 018package com.nimbusds.openid.connect.sdk.assurance.evidences; 019 020 021import net.minidev.json.JSONAware; 022import net.minidev.json.JSONObject; 023 024import com.nimbusds.oauth2.sdk.ParseException; 025import com.nimbusds.oauth2.sdk.util.JSONObjectUtils; 026 027 028/** 029 * The base abstract class for identity evidences. 030 * 031 * <p>Related specifications: 032 * 033 * <ul> 034 * <li>OpenID Connect for Identity Assurance 1.0, section 4.1.1. 035 * </ul> 036 */ 037public abstract class IdentityEvidence implements JSONAware { 038 039 040 /** 041 * The evidence type. 042 */ 043 private final IdentityEvidenceType evidenceType; 044 045 046 /** 047 * Creates a new evidence with the specified type. 048 * 049 * @param evidenceType The evidence type. Must not be {@code null}. 050 */ 051 protected IdentityEvidence(final IdentityEvidenceType evidenceType) { 052 if (evidenceType == null) { 053 throw new IllegalArgumentException("The evidence type must not be null"); 054 } 055 this.evidenceType = evidenceType; 056 } 057 058 059 /** 060 * Returns the evidence type. 061 * 062 * @return The evidence type. 063 */ 064 public IdentityEvidenceType getEvidenceType() { 065 return evidenceType; 066 } 067 068 069 /** 070 * Casts this identity evidence to an ID document evidence. 071 * 072 * @return The ID document evidence. 073 */ 074 public IDDocumentEvidence toIDDocumentEvidence() { 075 076 return (IDDocumentEvidence)this; 077 } 078 079 080 /** 081 * Casts this identity evidence to a utility bill evidence. 082 * 083 * @return The utility bill evidence. 084 */ 085 public UtilityBillEvidence toUtilityBillEvidence() { 086 087 return (UtilityBillEvidence)this; 088 } 089 090 091 /** 092 * Casts this identity evidence to a QES evidence. 093 * 094 * @return The QES evidence. 095 */ 096 public QESEvidence toQESEvidence() { 097 098 return (QESEvidence)this; 099 } 100 101 102 /** 103 * Returns a JSON object representation of this evidence. 104 * 105 * @return The JSON object. 106 */ 107 public JSONObject toJSONObject() { 108 109 JSONObject o = new JSONObject(); 110 o.put("type", getEvidenceType().getValue()); 111 return o; 112 } 113 114 115 @Override 116 public String toJSONString() { 117 return toJSONObject().toJSONString(); 118 } 119 120 121 /** 122 * Parses an identity evidence from the specified JSON object. 123 * 124 * @param jsonObject The JSON object. Must not be {@code null}. 125 * 126 * @return A {@link IDDocumentEvidence}, {@link QESEvidence} or 127 * {@link UtilityBillEvidence} instance. 128 * 129 * @throws ParseException If parsing failed or the evidence type isn't 130 * supported. 131 */ 132 public static IdentityEvidence parse(final JSONObject jsonObject) 133 throws ParseException { 134 135 IdentityEvidenceType type = new IdentityEvidenceType(JSONObjectUtils.getString(jsonObject, "type")); 136 137 if (IdentityEvidenceType.ID_DOCUMENT.equals(type)) { 138 return IDDocumentEvidence.parse(jsonObject); 139 140 } else if (IdentityEvidenceType.QES.equals(type)) { 141 return QESEvidence.parse(jsonObject); 142 143 } else if (IdentityEvidenceType.UTILITY_BILL.equals(type)) { 144 return UtilityBillEvidence.parse(jsonObject); 145 146 } else { 147 throw new ParseException("Unsupported type: " + type); 148 } 149 } 150 151 152 /** 153 * Ensures the {@code type} member of the specified JSON object matches 154 * the expected. 155 * 156 * @param expectedType The expected type. Must not be {@code null}. 157 * @param jsonObject The JSON object. Must not be {@code null}. 158 * 159 * @throws ParseException If parsing failed or mismatch. 160 */ 161 protected static void ensureType(final IdentityEvidenceType expectedType, JSONObject jsonObject) 162 throws ParseException { 163 164 String parsedType = JSONObjectUtils.getString(jsonObject, "type"); 165 166 if (! expectedType.getValue().equals(parsedType)) { 167 throw new ParseException("The identity evidence type must be " + expectedType); 168 } 169 } 170}