io.nats.client.impl.FileAuthHandler Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jnats Show documentation
Show all versions of jnats Show documentation
Client library for working with the NATS messaging system.
// Copyright 2018 The NATS Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at:
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package io.nats.client.impl;
import io.nats.client.AuthHandler;
import io.nats.client.NKey;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
class FileAuthHandler implements AuthHandler {
private String jwtFile;
private String nkeyFile;
private String credsFile;
FileAuthHandler(String creds) {
this.credsFile = creds;
}
FileAuthHandler(String jwtFile, String nkeyFile) {
this.jwtFile = jwtFile;
this.nkeyFile = nkeyFile;
}
private char[] extract(CharBuffer data, int headers) {
CharBuffer buff = CharBuffer.allocate(data.length());
boolean skipLine = false;
int headerCount = 0;
int linePos = -1;
while (data.length() > 0) {
char c = data.get();
linePos++;
// End of line, either we got it, or we should keep reading the new line
if (c == '\n' || c=='\r') {
if (buff.position() > 0) { // we wrote something
break;
}
skipLine = false;
linePos = -1; // so we can start right up
continue;
}
// skip to the new line
if (skipLine) {
continue;
}
// Ignore whitespace
if (Character.isWhitespace(c)) {
continue;
}
// If we are on a - skip that line, bump the header count
if (c == '-' && linePos == 0) {
skipLine = true;
headerCount++;
continue;
}
// Skip the line, or add to buff
if (!skipLine && headerCount==headers) {
buff.put(c);
}
}
// check for naked value
if (buff.position() == 0 && headers==1) {
data.position(0);
while (data.length() > 0) {
char c = data.get();
if (c == '\n' || c=='\r' || Character.isWhitespace(c)) {
if (buff.position() > 0) { // we wrote something
break;
}
continue;
}
buff.put(c);
}
buff.flip();
} else {
buff.flip();
}
char[] retVal = new char[buff.length()];
buff.get(retVal);
buff.clear();
for (int i=0; i
© 2015 - 2025 Weber Informatics LLC | Privacy Policy