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