2 * ============LICENSE_START====================================================
4 * ===========================================================================
5 * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved.
6 * ===========================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END====================================================
22 package org.onap.aaf.auth.dao.cass;
24 import java.io.ByteArrayOutputStream;
25 import java.io.DataInputStream;
26 import java.io.DataOutputStream;
27 import java.io.IOException;
28 import java.nio.ByteBuffer;
29 import java.util.Date;
30 import java.util.HashSet;
31 import java.util.List;
34 import org.onap.aaf.auth.dao.AbsCassDAO;
35 import org.onap.aaf.auth.dao.Bytification;
36 import org.onap.aaf.auth.dao.CassDAOImpl;
37 import org.onap.aaf.auth.dao.Loader;
38 import org.onap.aaf.auth.dao.Streamer;
39 import org.onap.aaf.auth.env.AuthzTrans;
40 import org.onap.aaf.auth.layer.Result;
41 import org.onap.aaf.misc.env.util.Chrono;
43 import com.datastax.driver.core.Cluster;
44 import com.datastax.driver.core.Row;
47 * CredDAO manages credentials.
51 public class OAuthTokenDAO extends CassDAOImpl<AuthzTrans,OAuthTokenDAO.Data> {
52 public static final String TABLE = "oauth_token";
53 private AbsCassDAO<AuthzTrans, Data>.PSInfo psByUser;
55 public OAuthTokenDAO(AuthzTrans trans, Cluster cluster, String keyspace) {
56 super(trans, OAuthTokenDAO.class.getSimpleName(),cluster, keyspace, Data.class,TABLE, readConsistency(trans,TABLE), writeConsistency(trans,TABLE));
60 public OAuthTokenDAO(AuthzTrans trans, AbsCassDAO<AuthzTrans,?> aDao) {
61 super(trans, OAuthTokenDAO.class.getSimpleName(),aDao, Data.class, TABLE, readConsistency(trans,TABLE), writeConsistency(trans,TABLE));
66 public static final int KEYLIMIT = 1;
67 public static class Data implements Bytification {
69 public String client_id;
71 public boolean active;
73 public String refresh;
76 public String content;
77 public Set<String> scopes;
79 public String req_ip; // requesting
81 public Set<String> scopes(boolean mutable) {
83 scopes = new HashSet<>();
84 } else if (mutable && !(scopes instanceof HashSet)) {
85 scopes = new HashSet<>(scopes);
91 public ByteBuffer bytify() throws IOException {
92 ByteArrayOutputStream baos = new ByteArrayOutputStream();
93 OAuthLoader.deflt.marshal(this,new DataOutputStream(baos));
94 return ByteBuffer.wrap(baos.toByteArray());
98 public void reconstitute(ByteBuffer bb) throws IOException {
99 OAuthLoader.deflt.unmarshal(this, toDIS(bb));
102 public String toString() {
103 return user.toString() + ' ' + id.toString() + ' ' + Chrono.dateTime(expires) + (active?"":"in") + "active";
107 private static class OAuthLoader extends Loader<Data> implements Streamer<Data>{
108 public static final int MAGIC=235677843;
109 public static final int VERSION=1;
110 public static final int BUFF_SIZE=96; // Note: only used when
112 public static final OAuthLoader deflt = new OAuthLoader(KEYLIMIT);
113 public OAuthLoader(int keylimit) {
118 public Data load(Data data, Row row) {
119 data.id = row.getString(0);
120 data.client_id = row.getString(1);
121 data.user = row.getString(2);
122 data.active = row.getBool(3);
123 data.type = row.getInt(4);
124 data.refresh = row.getString(5);
125 data.expires = row.getTimestamp(6);
126 data.exp_sec = row.getLong(7);
127 data.content = row.getString(8);
128 data.scopes = row.getSet(9,String.class);
129 data.state = row.getString(10);
130 data.req_ip = row.getString(11);
135 protected void key(final Data data, final int idx, Object[] obj) {
140 protected void body(final Data data, final int idx, Object[] obj) {
142 obj[i=idx] = data.client_id;
143 obj[++i] = data.user;
144 obj[++i] = data.active;
145 obj[++i] = data.type;
146 obj[++i] = data.refresh;
147 obj[++i] = data.expires;
148 obj[++i] = data.exp_sec;
149 obj[++i] = data.content;
150 obj[++i] = data.scopes;
151 obj[++i] = data.state;
152 obj[++i] = data.req_ip;
156 public void marshal(Data data, DataOutputStream os) throws IOException {
157 writeHeader(os,MAGIC,VERSION);
158 writeString(os, data.id);
159 writeString(os, data.client_id);
160 writeString(os, data.user);
161 os.writeBoolean(data.active);
162 os.writeInt(data.type);
163 writeString(os, data.refresh);
164 os.writeLong(data.expires==null?-1:data.expires.getTime());
165 os.writeLong(data.exp_sec);
166 writeString(os, data.content);
167 writeStringSet(os,data.scopes);
168 writeString(os, data.state);
169 writeString(os, data.req_ip);
174 public void unmarshal(Data data, DataInputStream is) throws IOException {
175 /*int version = */readHeader(is,MAGIC,VERSION);
176 // If Version Changes between Production runs, you'll need to do a switch Statement, and adequately read in fields
177 byte[] buff = new byte[BUFF_SIZE]; // used only if fits
178 data.id = readString(is,buff);
179 data.client_id = readString(is,buff);
180 data.user = readString(is,buff);
181 data.active = is.readBoolean();
182 data.type = is.readInt();
183 data.refresh = readString(is,buff);
184 long l = is.readLong();
185 data.expires = l<0?null:new Date(l);
186 data.exp_sec = is.readLong();
187 data.content = readString(is,buff); // note, large strings still ok with small buffer
188 data.scopes = readStringSet(is,buff);
189 data.state = readString(is,buff);
190 data.req_ip = readString(is,buff);
194 private void init(AuthzTrans trans) {
195 String[] helpers = setCRUD(trans, TABLE, Data.class, OAuthLoader.deflt);
196 psByUser = new PSInfo(trans, "SELECT " + helpers[0] + " from " + TABLE + " WHERE user=?",OAuthLoader.deflt,readConsistency);
200 * Log Modification statements to History
202 * @param modified which CRUD action was done
203 * @param data entity data that needs a log entry
204 * @param overrideMessage if this is specified, we use it rather than crafting a history message based on data
207 protected void wasModified(AuthzTrans trans, CRUD modified, Data data, String ... override) {
210 public Result<List<Data>> readByUser(AuthzTrans trans, String user) {
211 return psByUser.read(trans, "Read By User", new Object[]{user});